Completed
Push — master ( 2058f9...b13840 )
by Paweł
31:54
created

LoadCollectionRouteArticles::loadArticles()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 64
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 4

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 64
ccs 43
cts 43
cp 1
rs 8.8725
cc 4
eloc 44
nc 4
nop 2
crap 4

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
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 LoadCollectionRouteArticles extends AbstractFixture implements FixtureInterface, OrderedFixtureInterface
25
{
26
    private $manager;
27
    private $defaultTenantPrefix;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 6
    public function load(ObjectManager $manager)
33
    {
34 6
        $this->manager = $manager;
35 6
        $env = $this->getEnvironment();
36
37 6
        if ('test' !== $env) {
38
            return;
39
        }
40
41 6
        $this->defaultTenantPrefix = $this->getTenantPrefix();
42
43 6
        $this->loadRoutes($env, $manager);
44 6
        $this->loadArticles($env, $manager);
45 6
        $this->setRoutesContent($env, $manager);
46
47 6
        $manager->flush();
48 6
    }
49
50 6
    public function loadRoutes($env, ObjectManager $manager)
51
    {
52
        $routes = [
53
            'test' => [
54
                [
55 6
                    'parent' => $this->defaultTenantPrefix.'/routes',
56 6
                    'name' => 'collection-no-template',
57 6
                    'type' => 'collection',
58
                ],
59
                [
60 6
                    'parent' => $this->defaultTenantPrefix.'/routes',
61 6
                    'name' => 'collection-test',
62 6
                    'type' => 'collection',
63 6
                    'template_name' => 'collection.html.twig',
64
                ],
65
                [
66 6
                    'parent' => $this->defaultTenantPrefix.'/routes',
67 6
                    'name' => 'collection-content',
68 6
                    'type' => 'collection',
69 6
                    'articles_template_name' => 'test.html.twig',
70
                ],
71
            ],
72
        ];
73
74 6
        $routeService = $this->container->get('swp.service.route');
75
76 6
        foreach ($routes[$env] as $routeData) {
77 6
            $route = $routeService->createRoute($routeData);
78
79 6
            $manager->persist($route);
80
        }
81
82 6
        $manager->flush();
83 6
    }
84
85 6
    public function setRoutesContent($env, $manager)
86
    {
87
        $routes = [
88
            'test' => [
89
                [
90 6
                    'path' => $this->defaultTenantPrefix.'/routes/collection-content',
91 6
                    'content' => $this->defaultTenantPrefix.'/content/some-content',
92
                ],
93
            ],
94
        ];
95
96 6 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...
97 6
            if (array_key_exists('content', $routeData)) {
98 6
                $route = $manager->find(null, $routeData['path']);
99 6
                $route->setContent($manager->find(null, $routeData['content']));
100
            }
101
        }
102 6
    }
103
104 6
    public function loadArticles($env, $manager)
105
    {
106
        $articles = [
107
            'test' => [
108
                [
109 6
                    'title' => 'Test art1',
110 6
                    'content' => 'Test art1 content',
111 6
                    'route' => $this->defaultTenantPrefix.'/routes/collection-test',
112 6
                    'parent' => $this->defaultTenantPrefix.'/content',
113 6
                    'locale' => 'en',
114
                ],
115
                [
116 6
                    'title' => 'Test art2',
117 6
                    'content' => 'Test art2 content',
118 6
                    'route' => $this->defaultTenantPrefix.'/routes/collection-test',
119 6
                    'parent' => $this->defaultTenantPrefix.'/content',
120 6
                    'locale' => 'en',
121
                ],
122
                [
123 6
                    'title' => 'Test art3',
124 6
                    'content' => 'Test art3',
125 6
                    'route' => $this->defaultTenantPrefix.'/routes/collection-test',
126 6
                    'parent' => $this->defaultTenantPrefix.'/content',
127 6
                    'locale' => 'en',
128
                ],
129
                [
130 6
                    'title' => 'Some content',
131 6
                    'content' => 'some content',
132 6
                    'route' => $this->defaultTenantPrefix.'/routes/collection-content',
133 6
                    'parent' => $this->defaultTenantPrefix.'/content',
134 6
                    'locale' => 'en',
135 6
                    'templateName' => 'some_content.html.twig',
136
                ],
137
                [
138 6
                    'title' => 'Some other content',
139 6
                    'content' => 'some other content',
140 6
                    'route' => $this->defaultTenantPrefix.'/routes/collection-content',
141 6
                    'parent' => $this->defaultTenantPrefix.'/content',
142 6
                    'locale' => 'en',
143
                ],
144
            ],
145
        ];
146
147 6
        if (isset($articles[$env])) {
148 6
            foreach ($articles[$env] as $articleData) {
149 6
                $article = new Article();
150 6
                $article->setParentDocument($manager->find(null, $articleData['parent']));
151 6
                $article->setTitle($articleData['title']);
152 6
                $article->setBody($articleData['content']);
153 6
                $article->setRoute($manager->find(null, $articleData['route']));
154 6
                $article->setLocale($articleData['locale']);
155 6
                $article->setPublishedAt(new \DateTime());
156 6
                $article->setPublishable(true);
157 6
                $article->setStatus(ArticleInterface::STATUS_PUBLISHED);
158 6
                if (isset($articleData['templateName'])) {
159 6
                    $article->setTemplateName($articleData['templateName']);
160
                }
161
162 6
                $manager->persist($article);
163
            }
164
165 6
            $manager->flush();
166
        }
167 6
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172 6
    public function getOrder()
173
    {
174 6
        return 20;
175
    }
176
}
177