Completed
Push — master ( a682ba...4851b3 )
by Paweł
40:50
created

LoadArticlesData   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 199
Duplicated Lines 18.59 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 5
dl 37
loc 199
ccs 40
cts 48
cp 0.8333
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 10 1
B loadRoutes() 21 86 5
B loadArticles() 0 59 4
A articleMetadata() 16 16 1
A getOrder() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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