1 | <?php |
||
35 | class ArticleLoader extends PaginatedLoader implements LoaderInterface |
||
36 | { |
||
37 | /** |
||
38 | * @var ArticleProviderInterface |
||
39 | */ |
||
40 | protected $articleProvider; |
||
41 | |||
42 | /** |
||
43 | * @var RouteProviderInterface |
||
44 | */ |
||
45 | protected $routeProvider; |
||
46 | |||
47 | /** |
||
48 | * @var ObjectManager |
||
49 | */ |
||
50 | protected $dm; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $routeBasepaths; |
||
56 | |||
57 | /** |
||
58 | * @var MetaFactoryInterface |
||
59 | */ |
||
60 | protected $metaFactory; |
||
61 | |||
62 | /** |
||
63 | * @var Context |
||
64 | */ |
||
65 | protected $context; |
||
66 | |||
67 | /** |
||
68 | * ArticleLoader constructor. |
||
69 | * |
||
70 | * @param ArticleProviderInterface $articleProvider |
||
71 | * @param RouteProviderInterface $routeProvider |
||
72 | * @param ObjectManager $dm |
||
73 | * @param MetaFactoryInterface $metaFactory |
||
74 | * @param Context $context |
||
75 | */ |
||
76 | 105 | public function __construct( |
|
77 | ArticleProviderInterface $articleProvider, |
||
78 | RouteProviderInterface $routeProvider, |
||
79 | ObjectManager $dm, |
||
80 | MetaFactoryInterface $metaFactory, |
||
81 | Context $context |
||
82 | ) { |
||
83 | 105 | $this->articleProvider = $articleProvider; |
|
84 | 105 | $this->routeProvider = $routeProvider; |
|
85 | 105 | $this->dm = $dm; |
|
86 | 105 | $this->metaFactory = $metaFactory; |
|
87 | 105 | $this->context = $context; |
|
88 | 105 | } |
|
89 | |||
90 | /** |
||
91 | * Load meta object by provided type and parameters. |
||
92 | * |
||
93 | * @MetaLoaderDoc( |
||
94 | * description="Article Loader loads articles from Content Repository", |
||
95 | * parameters={ |
||
96 | * contentPath="SINGLE|required content path", |
||
97 | * slug="SINGLE|required content slug", |
||
98 | * pageName="COLLECTiON|name of Page for required articles" |
||
99 | * } |
||
100 | * ) |
||
101 | * |
||
102 | * @param string $type object type |
||
103 | * @param array $parameters parameters needed to load required object type |
||
104 | * @param int $responseType response type: single meta (LoaderInterface::SINGLE) or collection of metas (LoaderInterface::COLLECTION) |
||
105 | * |
||
106 | * @return Meta|Meta[]|bool false if meta cannot be loaded, a Meta instance otherwise |
||
107 | * |
||
108 | * @throws \Exception |
||
109 | */ |
||
110 | 18 | public function load($type, $parameters = [], $responseType = LoaderInterface::SINGLE) |
|
111 | { |
||
112 | 18 | $criteria = new Criteria(); |
|
113 | 18 | if ($responseType === LoaderInterface::SINGLE) { |
|
114 | 14 | if (array_key_exists('article', $parameters) && $parameters['article'] instanceof ArticleInterface) { |
|
115 | $this->dm->detach($parameters['article']); |
||
116 | $criteria->set('id', $parameters['article']->getId()); |
||
117 | 14 | } elseif (array_key_exists('slug', $parameters)) { |
|
118 | 14 | $criteria->set('slug', $parameters['slug']); |
|
119 | } |
||
120 | |||
121 | try { |
||
122 | 14 | return $this->getArticleMeta($this->articleProvider->getOneByCriteria($criteria)); |
|
123 | 4 | } catch (NotFoundHttpException $e) { |
|
124 | 4 | return; |
|
125 | } |
||
126 | 6 | } elseif ($responseType === LoaderInterface::COLLECTION) { |
|
127 | 6 | $currentPage = $this->context->getCurrentPage(); |
|
128 | 6 | $route = null; |
|
129 | |||
130 | 6 | if (null !== $currentPage) { |
|
131 | 2 | $route = $currentPage->getValues(); |
|
132 | } |
||
133 | |||
134 | 6 | if (array_key_exists('route', $parameters)) { |
|
135 | 6 | if (null === $route || ($route instanceof RouteInterface && $route->getId() !== $parameters['route'])) { |
|
136 | 6 | if (is_int($parameters['route'])) { |
|
137 | 1 | $route = $this->routeProvider->getOneById($parameters['route']); |
|
138 | 6 | } elseif (is_string($parameters['route'])) { |
|
139 | 6 | $route = $this->routeProvider->getOneByStaticPrefix($parameters['route']); |
|
140 | } |
||
141 | } |
||
142 | } |
||
143 | |||
144 | 6 | if ($route instanceof RouteInterface) { |
|
145 | 6 | $criteria->set('route', $route); |
|
146 | } else { |
||
147 | 1 | return; |
|
148 | } |
||
149 | |||
150 | 6 | $criteria = $this->applyPaginationToCriteria($criteria, $parameters); |
|
151 | 6 | $articles = $this->articleProvider->getManyByCriteria($criteria); |
|
152 | 5 | if ($articles->count() > 0) { |
|
153 | 5 | $metaCollection = new MetaCollection(); |
|
154 | 5 | $metaCollection->setTotalItemsCount($this->articleProvider->getCountByCriteria($criteria)); |
|
155 | 5 | foreach ($articles as $article) { |
|
156 | 5 | $articleMeta = $this->getArticleMeta($article); |
|
157 | 5 | if (null !== $articleMeta) { |
|
158 | 5 | $metaCollection->add($articleMeta); |
|
159 | } |
||
160 | } |
||
161 | 5 | unset($articles, $route, $criteria); |
|
162 | |||
163 | 5 | return $metaCollection; |
|
164 | } |
||
165 | } |
||
166 | |||
167 | return; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Checks if Loader supports provided type. |
||
172 | * |
||
173 | * @param string $type |
||
174 | * |
||
175 | * @return bool |
||
176 | */ |
||
177 | 17 | public function isSupported(string $type) : bool |
|
181 | |||
182 | 15 | private function getArticleMeta($article) |
|
183 | { |
||
190 | } |
||
191 |