1+ <?php
2+ /**
3+ * Copyright © Magento, Inc. All rights reserved.
4+ * See COPYING.txt for license details.
5+ */
6+ declare (strict_types=1 );
7+
8+ namespace Lof \ProductTagsGraphQl \Model \Resolver ;
9+
10+ use Lof \ProductTagsGraphQl \Model \Resolver \DataProvider \Tag as TagDataProvider ;
11+ use Magento \Framework \Exception \NoSuchEntityException ;
12+ use Magento \Framework \GraphQl \Config \Element \Field ;
13+ use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
14+ use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
15+ use Magento \Framework \GraphQl \Query \ResolverInterface ;
16+ use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
17+
18+ class Tag implements ResolverInterface
19+ {
20+ /**
21+ * @var TagDataProvider
22+ */
23+ private $ tagDataProvider ;
24+
25+ /**
26+ * @param DataProvider\Tag $tagRepository
27+ */
28+ public function __construct (DataProvider \Tag $ tagDataProvider )
29+ {
30+ $ this ->tagDataProvider = $ tagDataProvider ;
31+ }
32+
33+ /**
34+ * @inheritdoc
35+ */
36+ public function resolve (
37+ Field $ field ,
38+ $ context ,
39+ ResolveInfo $ info ,
40+ array $ value = null ,
41+ array $ args = null
42+ ) {
43+ $ tagId = $ this ->getTagId ($ args );
44+ $ tagData = $ this ->getTagData ($ tagId );
45+
46+ return $ tagData ;
47+ }
48+
49+ /**
50+ * @param array $args
51+ * @return int
52+ * @throws GraphQlInputException
53+ */
54+ private function getTagId (array $ args ): int
55+ {
56+ if (!isset ($ args ['id ' ])) {
57+ throw new GraphQlInputException (__ ('"Tag id should be specified ' ));
58+ }
59+
60+ return (int )$ args ['id ' ];
61+ }
62+
63+ /**
64+ * @param int $tagId
65+ * @return array
66+ * @throws GraphQlNoSuchEntityException
67+ */
68+ private function getTagData (int $ tagId ): array
69+ {
70+ try {
71+ $ tagData = $ this ->tagDataProvider ->getData ($ tagId );
72+ } catch (NoSuchEntityException $ e ) {
73+ throw new GraphQlNoSuchEntityException (__ ($ e ->getMessage ()), $ e );
74+ }
75+ return $ tagData ;
76+ }
77+ }
0 commit comments