Completed
Pull Request — master (#129)
by Paweł
28:59
created

LoadArticlesData::getOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * This file is part of the Superdesk Web Publisher Fixtures Bundle.
5
 *
6
 * Copyright 2015 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú.
12
 * @license http://www.superdesk.org/license
13
 */
14
namespace SWP\Bundle\FixturesBundle\DataFixtures\PHPCR;
15
16
use Doctrine\Common\DataFixtures\FixtureInterface;
17
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
18
use Doctrine\Common\Persistence\ObjectManager;
19
use SWP\Bundle\ContentBundle\Doctrine\ODM\PHPCR\Article;
20
use SWP\Bundle\FixturesBundle\AbstractFixture;
21
use SWP\Bundle\ContentBundle\Doctrine\ODM\PHPCR\Route;
22
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
23
24
class LoadArticlesData extends AbstractFixture implements FixtureInterface, OrderedFixtureInterface
25
{
26
    private $manager;
27
    private $defaultTenantPrefix;
28
    private $firstTenantPrefix;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 12
    public function load(ObjectManager $manager)
34
    {
35 12
        $this->manager = $manager;
36 12
        $env = $this->getEnvironment();
37
38 12
        $this->defaultTenantPrefix = $this->getTenantPrefix();
39 12
        $this->firstTenantPrefix = $this->getTenantPrefix('client1');
40
41 12
        $this->loadRoutes($env, $manager);
42 12
        $this->loadArticles($env, $manager);
43 12
        $this->setRoutesContent($env, $manager);
44
45 12
        $manager->flush();
46 12
    }
47
48 12
    public function loadRoutes($env, $manager)
49
    {
50
        $routes = [
51
            'dev' => [
52
                [
53 12
                    'parent' => $this->defaultTenantPrefix.'/routes',
54 12
                    'name' => 'news',
55 12
                    'variablePattern' => '/{slug}',
56
                    'requirements' => [
57
                        'slug' => '[a-zA-Z1-9\-_\/]+',
58
                    ],
59 12
                    'type' => 'collection',
60
                    'defaults' => [
61
                        'slug' => null,
62
                    ],
63 12
                    'templateName' => 'news.html.twig',
64 12
                    'articlesTemplateName' => 'article.html.twig',
65
                ],
66
                [
67 12
                    'parent' => $this->defaultTenantPrefix.'/routes',
68 12
                    'name' => 'articles',
69 12
                    'type' => 'collection',
70 12
                    'variablePattern' => '/{slug}',
71
                    'requirements' => [
72
                        'slug' => '[a-zA-Z1-9\-_\/]+',
73
                    ],
74 12
                    'type' => 'collection',
75
                    'defaults' => [
76
                        'slug' => null,
77
                    ],
78
                ],
79
                [
80 12
                    'parent' => $this->defaultTenantPrefix.'/routes/articles',
81 12
                    'name' => 'get-involved',
82 12
                    'type' => 'content',
83
                ],
84
                [
85 12
                    'parent' => $this->defaultTenantPrefix.'/routes/articles',
86 12
                    'name' => 'features',
87 12
                    'type' => 'content',
88
                ],
89 12
            ],
90
            'test' => [
91
                [
92 12
                    'parent' => $this->defaultTenantPrefix.'/routes',
93 12
                    'name' => 'news',
94 12
                    'variablePattern' => '/{slug}',
95
                    'requirements' => [
96
                        'slug' => '[a-zA-Z1-9\-_\/]+',
97
                    ],
98 12
                    'type' => 'collection',
99
                    'defaults' => [
100
                        'slug' => null,
101
                    ],
102
                ],
103
                [
104 12
                    'parent' => $this->firstTenantPrefix.'/routes',
105 12
                    'name' => 'news',
106 12
                    'variablePattern' => '/{slug}',
107
                    'requirements' => [
108
                        'slug' => '[a-zA-Z1-9\-_\/]+',
109
                    ],
110 12
                    'type' => 'collection',
111
                    'defaults' => [
112
                        'slug' => null,
113
                    ],
114
                ],
115
                [
116 12
                    'parent' => $this->defaultTenantPrefix.'/routes',
117 12
                    'name' => 'articles',
118 12
                    'type' => 'content',
119
                ],
120
                [
121 12
                    'parent' => $this->defaultTenantPrefix.'/routes/articles',
122 12
                    'name' => 'features',
123 12
                    'type' => 'content',
124
                ],
125
                [
126 12
                    'parent' => $this->firstTenantPrefix.'/routes',
127 12
                    'name' => 'features',
128 12
                    'type' => 'content',
129
                ],
130
            ],
131
        ];
132
133 12
        foreach ($routes[$env] as $routeData) {
134 12
            $route = new Route();
135 12
            $route->setParentDocument($manager->find(null, $routeData['parent']));
136 12
            $route->setName($routeData['name']);
137 12
            $route->setType($routeData['type']);
138
139 12
            if (isset($routeData['cacheTimeInSeconds'])) {
140
                $route->setCacheTimeInSeconds($routeData['cacheTimeInSeconds']);
141
            }
142
143 12
            if (isset($routeData['variablePattern'])) {
144 12
                $route->setVariablePattern($routeData['variablePattern']);
145
            }
146
147 12
            if (isset($routeData['requirements'])) {
148 12
                foreach ($routeData['requirements'] as $key => $value) {
149 12
                    $route->setRequirement($key, $value);
150
                }
151
            }
152
153 12
            if (isset($routeData['templateName'])) {
154
                $route->setTemplateName($routeData['templateName']);
155
            }
156
157 12
            if (isset($routeData['articlesTemplateName'])) {
158
                $route->setArticlesTemplateName($routeData['articlesTemplateName']);
159
            }
160
161 12
            if (isset($routeData['defaults'])) {
162 12
                foreach ($routeData['defaults'] as $key => $value) {
163 12
                    $route->setDefault($key, $value);
164
                }
165
            }
166 12
            $manager->persist($route);
167
        }
168
169 12
        $manager->flush();
170 12
    }
171
172 12
    public function setRoutesContent($env, $manager)
173
    {
174
        $routes = [
175
            'dev' => [
176
                [
177 12
                    'path' => $this->defaultTenantPrefix.'/routes/articles/features',
178 12
                    'content' => $this->defaultTenantPrefix.'/content/features',
179
                ],
180
                [
181 12
                    'path' => $this->defaultTenantPrefix.'/routes/articles/get-involved',
182 12
                    'content' => $this->defaultTenantPrefix.'/content/get-involved',
183
                ],
184
            ],
185
            'test' => [
186
                [
187 12
                    'path' => $this->defaultTenantPrefix.'/routes/news',
188 12
                    'content' => $this->defaultTenantPrefix.'/content/test-news-article',
189
                ],
190
                [
191 12
                    'path' => $this->defaultTenantPrefix.'/routes/articles/features',
192 12
                    'content' => $this->defaultTenantPrefix.'/content/features',
193
                ],
194
                [
195 12
                    'path' => $this->firstTenantPrefix.'/routes/features',
196 12
                    'content' => $this->firstTenantPrefix.'/content/features-client1',
197
                ],
198
            ],
199
        ];
200
201 12 View Code Duplication
        foreach ($routes[$env] as $routeData) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
202 12
            if (array_key_exists('content', $routeData)) {
203 12
                $route = $manager->find(null, $routeData['path']);
204 12
                $route->setContent($manager->find(null, $routeData['content']));
205
            }
206
        }
207 12
    }
208
209
    /**
210
     * Sets articles manually (not via Alice) for test env due to fatal error:
211
     * Method PHPCRProxies\__CG__\Doctrine\ODM\PHPCR\Document\Generic::__toString() must not throw an exception.
212
     */
213 12
    public function loadArticles($env, $manager)
214
    {
215 12
        if ($env !== 'test') {
216
            $this->loadFixtures(
217
                '@SWPFixturesBundle/Resources/fixtures/PHPCR/'.$env.'/article.yml',
218
                $manager,
219
                [
220
                    'providers' => [$this],
221
                ]
222
            );
223
        }
224
225
        $articles = [
226
            'test' => [
227
                [
228 12
                    'title' => 'Test news article',
229 12
                    'content' => 'Test news article content',
230 12
                    'route' => $this->defaultTenantPrefix.'/routes/news',
231 12
                    'parent' => $this->defaultTenantPrefix.'/content',
232 12
                    'locale' => 'en',
233
                ],
234
                [
235 12
                    'title' => 'Test article',
236 12
                    'content' => 'Test article content',
237 12
                    'route' => $this->defaultTenantPrefix.'/routes/news',
238 12
                    'parent' => $this->defaultTenantPrefix.'/content',
239 12
                    'locale' => 'en',
240
                ],
241
                [
242 12
                    'title' => 'Features',
243 12
                    'content' => 'Features content',
244 12
                    'route' => $this->defaultTenantPrefix.'/routes/news',
245 12
                    'parent' => $this->defaultTenantPrefix.'/content',
246 12
                    'locale' => 'en',
247
                ],
248
                [
249 12
                    'title' => 'Features client1',
250 12
                    'content' => 'Features client1 content',
251 12
                    'route' => $this->firstTenantPrefix.'/routes/news',
252 12
                    'parent' => $this->firstTenantPrefix.'/content',
253 12
                    'locale' => 'en',
254
                ],
255
            ],
256
        ];
257
258 12
        if (isset($articles[$env])) {
259 12
            foreach ($articles[$env] as $articleData) {
260 12
                $article = new Article();
261 12
                $article->setParentDocument($manager->find(null, $articleData['parent']));
262 12
                $article->setTitle($articleData['title']);
263 12
                $article->setBody($articleData['content']);
264 12
                $article->setRoute($manager->find(null, $articleData['route']));
265 12
                $article->setLocale($articleData['locale']);
266 12
                $article->setPublishedAt(new \DateTime());
267 12
                $article->setPublishable(true);
268 12
                $article->setStatus(ArticleInterface::STATUS_PUBLISHED);
269
270 12
                $manager->persist($article);
271
            }
272
273 12
            $manager->flush();
274
        }
275 12
    }
276
277
    /**
278
     * Article example metadata.
279
     *
280
     * @return array
281
     */
282
    public function articleMetadata()
283
    {
284
        return [
285
            'located' => 'Sydney',
286
            'byline' => 'Jhon Doe',
287
            'place' => [
288
                [
289
                    'qcode' => 'AUS',
290
                    'world_region' => 'Rest Of World',
291
                ], [
292
                    'qcode' => 'EUR',
293
                    'world_region' => 'Europe',
294
                ],
295
            ],
296
        ];
297
    }
298
299
    /**
300
     * {@inheritdoc}
301
     */
302 12
    public function getOrder()
303
    {
304 12
        return 1;
305
    }
306
}
307