Completed
Push — master ( 3460d4...501fc3 )
by Paweł
76:02 queued 29:07
created

LoadArticlesData::loadRoutes()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 86
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5.1374

Importance

Changes 0
Metric Value
dl 0
loc 86
ccs 14
cts 17
cp 0.8235
rs 8.3234
c 0
b 0
f 0
cc 5
eloc 49
nc 9
nop 2
crap 5.1374

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
15
namespace SWP\Bundle\FixturesBundle\DataFixtures\ORM;
16
17
use Doctrine\Common\DataFixtures\FixtureInterface;
18
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
19
use Doctrine\Common\Persistence\ObjectManager;
20
use SWP\Bundle\FixturesBundle\AbstractFixture;
21
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
22
23
class LoadArticlesData extends AbstractFixture implements FixtureInterface, OrderedFixtureInterface
24
{
25
    private $manager;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 5
    public function load(ObjectManager $manager)
31
    {
32 5
        $this->manager = $manager;
33 5
        $env = $this->getEnvironment();
34
35 5
        $this->loadRoutes($env, $manager);
36 5
        $this->loadArticles($env, $manager);
37
38 5
        $manager->flush();
39 5
    }
40
41 5
    public function loadRoutes($env, $manager)
42
    {
43
        $routes = [
44
            'dev' => [
45
                [
46
                    'name' => 'news',
47
                    'variablePattern' => '/{slug}',
48
                    'requirements' => [
49
                        'slug' => '[a-zA-Z0-9*\-_\/]+',
50
                    ],
51
                    'type' => 'collection',
52
                    'defaults' => [
53
                        'slug' => null,
54
                    ],
55
                    'templateName' => 'news.html.twig',
56
                    'articlesTemplateName' => 'article.html.twig',
57
                ],
58
                [
59
                    'name' => 'articles',
60
                    'type' => 'collection',
61
                    'variablePattern' => '/{slug}',
62
                    'requirements' => [
63
                        'slug' => '[a-zA-Z1-9*\-_\/]+',
64
                    ],
65
                    'defaults' => [
66
                        'slug' => null,
67
                    ],
68
                ],
69
                [
70
                    'name' => 'get-involved',
71
                    'type' => 'content',
72
                ],
73
                [
74
                    'name' => 'features',
75
                    'type' => 'content',
76
                ],
77 5
            ],
78
            'test' => [
79
                [
80
                    'name' => 'news',
81
                    'variablePattern' => '/{slug}',
82
                    'requirements' => [
83
                        'slug' => '[a-zA-Z0-9*\-_\/]+',
84
                    ],
85
                    'type' => 'collection',
86
                    'defaults' => [
87
                        'slug' => null,
88
                    ],
89
                ],
90
                [
91
                    'name' => 'articles',
92
                    'type' => 'content',
93
                ],
94
                [
95
                    'name' => 'articles/features',
96
                    'type' => 'content',
97
                ],
98
            ],
99
        ];
100
101 5
        $routeService = $this->container->get('swp.service.route');
102
103 5
        foreach ($routes[$env] as $routeData) {
104 5
            $route = $this->container->get('swp.factory.route')->create();
105 5
            $route->setName($routeData['name']);
106 5
            $route->setType($routeData['type']);
107
108 5
            if (isset($routeData['cacheTimeInSeconds'])) {
109
                $route->setCacheTimeInSeconds($routeData['cacheTimeInSeconds']);
110
            }
111
112 5
            if (isset($routeData['templateName'])) {
113
                $route->setTemplateName($routeData['templateName']);
114
            }
115
116 5
            if (isset($routeData['articlesTemplateName'])) {
117
                $route->setArticlesTemplateName($routeData['articlesTemplateName']);
118
            }
119
120 5
            $route = $routeService->fillRoute($route);
121
122 5
            $manager->persist($route);
123
        }
124
125 5
        $manager->flush();
126 5
    }
127
128
    /**
129
     * Sets articles manually (not via Alice) for test env due to fatal error:
130
     * Method PHPCRProxies\__CG__\Doctrine\ODM\PHPCR\Document\Generic::__toString() must not throw an exception.
131
     */
132 5
    public function loadArticles($env, ObjectManager $manager)
133
    {
134 5
        if ($env !== 'test') {
135
            $this->loadFixtures(
136
                '@SWPFixturesBundle/Resources/fixtures/ORM/'.$env.'/article.yml',
137
                $manager,
138
                [
139
                    'providers' => [$this],
140
                ]
141
            );
142
        }
143
144
        $articles = [
145
            'test' => [
146
                [
147
                    'title' => 'Test news article',
148
                    'content' => 'Test news article content',
149
                    'route' => 'news',
150
                    'locale' => 'en',
151
                ],
152
                [
153
                    'title' => 'Test article',
154
                    'content' => 'Test article content',
155
                    'route' => 'news',
156
                    'locale' => 'en',
157
                ],
158
                [
159
                    'title' => 'Features',
160
                    'content' => 'Features content',
161
                    'route' => 'news',
162
                    'locale' => 'en',
163
                ],
164
                [
165
                    'title' => 'Features client1',
166
                    'content' => 'Features client1 content',
167
                    'route' => 'articles/features',
168
                    'locale' => 'en',
169
                ],
170 5
            ],
171
        ];
172
173 5
        if (isset($articles[$env])) {
174 5
            foreach ($articles[$env] as $articleData) {
175 5
                $article = $this->container->get('swp.factory.article')->create();
176 5
                $article->setTitle($articleData['title']);
177 5
                $article->setBody($articleData['content']);
178 5
                $article->setRoute($this->getRouteByName($articleData['route']));
179 5
                $article->setLocale($articleData['locale']);
180 5
                $article->setPublishable(true);
181 5
                $article->setPublishedAt(new \DateTime());
182 5
                $article->setStatus(ArticleInterface::STATUS_PUBLISHED);
183 5
                $manager->persist($article);
184
185 5
                $this->addReference($article->getSlug(), $article);
186
            }
187
188 5
            $manager->flush();
189
        }
190 5
    }
191
192
    /**
193
     * Article example metadata.
194
     *
195
     * @return array
196
     */
197
    public function articleMetadata()
198
    {
199
        return [
200
            'located' => 'Sydney',
201
            'byline' => 'Jhon Doe',
202
            'place' => [
203
                [
204
                    'qcode' => 'AUS',
205
                    'world_region' => 'Rest Of World',
206
                ], [
207
                    'qcode' => 'EUR',
208
                    'world_region' => 'Europe',
209
                ],
210
            ],
211
        ];
212
    }
213
214
    /**
215
     * {@inheritdoc}
216
     */
217 5
    public function getOrder()
218
    {
219 5
        return 1;
220
    }
221
}
222