66namespace BD \EzPlatformGraphQLBundle \GraphQL \Resolver ;
77
88use eZ \Publish \API \Repository \ContentService ;
9+ use eZ \Publish \Core \FieldType ;
910use eZ \Publish \SPI \Variation \VariationHandler ;
1011use Overblog \GraphQLBundle \Error \UserError ;
1112use eZ \Publish \Core \FieldType \Image \Value as ImageFieldValue ;
@@ -25,29 +26,45 @@ class ImageFieldResolver
2526 * @var array
2627 */
2728 private $ variations ;
29+ /**
30+ * @var FieldType\Image\Type
31+ */
32+ private $ fieldType ;
2833
29- public function __construct (VariationHandler $ variationHandler , ContentService $ contentService , array $ variations )
34+ public function __construct (
35+ FieldType \Image \Type $ imageFieldType ,
36+ VariationHandler $ variationHandler ,
37+ ContentService $ contentService ,
38+ array $ variations
39+ )
3040 {
3141 $ this ->variationHandler = $ variationHandler ;
3242 $ this ->contentService = $ contentService ;
3343 $ this ->variations = $ variations ;
44+ $ this ->fieldType = $ imageFieldType ;
3445 }
3546
3647 public function resolveImageVariations (ImageFieldValue $ fieldValue , $ args )
3748 {
49+ if ($ this ->fieldType ->isEmptyValue ($ fieldValue )) {
50+ return null ;
51+ }
3852 list ($ content , $ field ) = $ this ->getImageField ($ fieldValue );
3953
4054 $ variations = [];
4155 foreach ($ args ['identifier ' ] as $ identifier ) {
42- $ versionInfo = $ this ->contentService ->loadVersionInfo ($ content ->contentInfo );
43- $ variations [] = $ this ->variationHandler ->getVariation ($ field , $ versionInfo , $ identifier );
56+ $ variations [] = $ this ->variationHandler ->getVariation ($ field , $ content ->versionInfo , $ identifier );
4457 }
4558
4659 return $ variations ;
4760 }
4861
4962 public function resolveImageVariation (ImageFieldValue $ fieldValue , $ args )
5063 {
64+ if ($ this ->fieldType ->isEmptyValue ($ fieldValue )) {
65+ return null ;
66+ }
67+
5168 list ($ content , $ field ) = $ this ->getImageField ($ fieldValue );
5269 $ versionInfo = $ this ->contentService ->loadVersionInfo ($ content ->contentInfo );
5370
@@ -56,17 +73,13 @@ public function resolveImageVariation(ImageFieldValue $fieldValue, $args)
5673
5774 /**
5875 * @param ImageFieldValue $fieldValue
59- * @return array
76+ * @return [Content, Field]
6077 * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
6178 * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
6279 */
6380 protected function getImageField (ImageFieldValue $ fieldValue ): array
6481 {
65- $ idArray = explode ('- ' , $ fieldValue ->imageId );
66- if (count ($ idArray ) != 3 ) {
67- throw new UserError ("Invalid image ID {$ fieldValue ->imageId }" );
68- }
69- list ($ contentId , $ fieldId , $ versionNumber ) = $ idArray ;
82+ list ($ contentId , $ fieldId , $ versionNumber ) = $ this ->decomposeImageId ($ fieldValue );
7083
7184 $ content = $ this ->contentService ->loadContent ($ contentId , [], $ versionNumber );
7285
@@ -90,4 +103,17 @@ protected function getImageField(ImageFieldValue $fieldValue): array
90103
91104 return array ($ content , $ field );
92105 }
106+
107+ /**
108+ * @param ImageFieldValue $fieldValue
109+ * @return array
110+ */
111+ protected function decomposeImageId (ImageFieldValue $ fieldValue ): array
112+ {
113+ $ idArray = explode ('- ' , $ fieldValue ->imageId );
114+ if (count ($ idArray ) != 3 ) {
115+ throw new UserError ("Invalid image ID {$ fieldValue ->imageId }" );
116+ }
117+ return $ idArray ;
118+ }
93119}
0 commit comments