Source of file UpdateFileMutationCreator.php
Size: 2,302 Bytes - Last Modified: 2021-12-23T10:27:34+00:00
/var/www/docs.ssmods.com/process/src/_legacy/GraphQL/UpdateFileMutationCreator.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 | <?php namespace SilverStripe\AssetAdmin\GraphQL; use GraphQL\Type\Definition\ResolveInfo; use SilverStripe\Assets\File; use GraphQL\Type\Definition\Type; use SilverStripe\GraphQL\MutationCreator; use SilverStripe\GraphQL\OperationResolver; use SilverStripe\Versioned\Versioned; use SilverStripe\GraphQL\Util\CaseInsensitiveFieldAccessor; use SilverStripe\GraphQL\Manager; if (!class_exists(MutationCreator::class)) { return; } /** * Handles create and update * @skipUpgrade * @deprecated 4.8..5.0 Use silverstripe/graphql:^4 functionality. */ class UpdateFileMutationCreator extends MutationCreator implements OperationResolver { /** * @var CaseInsensitiveFieldAccessor */ protected $accessor; public function __construct(Manager $manager = null) { $this->accessor = new CaseInsensitiveFieldAccessor(); parent::__construct($manager); } public function attributes() { return [ 'name' => 'updateFile' ]; } public function type() { return $this->manager->getType('File'); } public function args() { return [ 'id' => [ 'type' => Type::nonNull(Type::id()), ], 'file' => [ 'type' => $this->manager->getType('FileInput') ], ]; } public function resolve($object, array $args, $context, ResolveInfo $info) { /** @var File $file */ $file = Versioned::get_by_stage(File::class, Versioned::DRAFT) ->byID($args['id']); if (!$file) { throw new \InvalidArgumentException(sprintf( '%s#%s not found', File::class, $args['id'] )); } if (!$file->canEdit($context['currentUser'])) { throw new \InvalidArgumentException(sprintf( '%s#%s update not allowed', File::class, $args['id'] )); } // TODO Use input type (and don't allow setting ID) foreach ($args['file'] as $name => $val) { $this->accessor->setValue($file, $name, $val); } $file->update($args); $file->writeToStage(Versioned::DRAFT); return $file; } } |