Passed
Push — master ( 501fc3...f64e27 )
by Paweł
47:53
created

LoadAmpHtmlData::load()   C

Complexity

Conditions 7
Paths 19

Size

Total Lines 103
Code Lines 66

Duplication

Lines 35
Ratio 33.98 %

Code Coverage

Tests 34
CRAP Score 7

Importance

Changes 0
Metric Value
dl 35
loc 103
ccs 34
cts 34
cp 1
rs 6.4589
c 0
b 0
f 0
cc 7
eloc 66
nc 19
nop 1
crap 7

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
namespace SWP\Bundle\FixturesBundle\DataFixtures\ORM;
4
5
use Doctrine\Common\DataFixtures\FixtureInterface;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
8
use SWP\Bundle\FixturesBundle\AbstractFixture;
9
10
class LoadAmpHtmlData extends AbstractFixture implements FixtureInterface
11
{
12
    private $manager;
13
14
    /**
15
     * {@inheritdoc}
16
     */
17 3
    public function load(ObjectManager $manager)
18
    {
19 3
        $this->manager = $manager;
20 3
        $env = $this->getEnvironment();
21
22 3
        if ($env === 'test') {
23
            $routes = [
24
                [
25
                    'name' => 'amp-articles',
26
                    'variablePattern' => '/{slug}',
27
                    'requirements' => [
28
                        'slug' => '[a-zA-Z0-9*\-_\/]+',
29
                    ],
30
                    'type' => 'collection',
31
                    'defaults' => [
32
                        'slug' => null,
33
                    ],
34
                    'templateName' => 'news.html.twig',
35
                    'articlesTemplateName' => 'article.html.twig',
36
                    'tenant' => '123abc',
37 3
                ],
38
                [
39
                    'name' => 'amp-articles-tenant-2',
40
                    'variablePattern' => '/{slug}',
41
                    'requirements' => [
42
                        'slug' => '[a-zA-Z0-9*\-_\/]+',
43
                    ],
44
                    'type' => 'collection',
45
                    'defaults' => [
46
                        'slug' => null,
47
                    ],
48
                    'templateName' => 'news.html.twig',
49
                    'articlesTemplateName' => 'article.html.twig',
50
                    'tenant' => '456def',
51
                ],
52
                [
53
                    'name' => 'some-content',
54
                    'type' => 'content',
55
                    'tenant' => '123abc',
56
                ],
57
            ];
58
59 3
            $routeService = $this->container->get('swp.service.route');
60
61 3 View Code Duplication
            foreach ($routes 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...
62 3
                $route = $this->container->get('swp.factory.route')->create();
63 3
                $route->setName($routeData['name']);
64 3
                $route->setType($routeData['type']);
65
66 3
                if (isset($routeData['templateName'])) {
67 3
                    $route->setTemplateName($routeData['templateName']);
68
                }
69
70 3
                if (isset($routeData['articlesTemplateName'])) {
71 3
                    $route->setArticlesTemplateName($routeData['articlesTemplateName']);
72
                }
73
74 3
                if (isset($routeData['tenant'])) {
75 3
                    $route->setTenantCode($routeData['tenant']);
76
                }
77
78 3
                $route = $routeService->fillRoute($route);
79
80 3
                $manager->persist($route);
81
            }
82
83 3
            $manager->flush();
84
85
            $articles = [
86
                [
87
                    'title' => 'AMP Html Article',
88
                    'content' => 'Nihil repellat vero omnis voluptates id amet et. Suscipit qui recusandae totam nulla quam ipsam. Cupiditate sed natus debitis voluptas aut. Sit repudiandae esse perspiciatis dignissimos error. Itaque quibusdam tempora velit porro ut velit soluta. Eligendi occaecati debitis et saepe. Sint dolorem delectus enim ipsum inventore sed libero. Velit qui suscipit a deserunt laudantium quibusdam enim. Soluta qui ipsam non ipsum. Reiciendis aperiam et fuga doloribus nisi. <iframe src="https://www.facebook.com/plugins/post.php?href=https%3A%2F%2Fwww.facebook.com%2Fniemanlab%2Fposts%2F10154594541763654&width=500" width="500" height="482" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true"></iframe>',
89
                    'route' => 'amp-articles',
90
                    'locale' => 'en',
91
                    'tenant' => '123abc',
92 3
                ],
93
                [
94
                    'title' => 'AMP Html Article Tenant 2',
95
                    'content' => 'Nihil repellat vero omnis voluptates id amet et. Suscipit qui recusandae totam nulla quam ipsam. Cupiditate sed natus debitis voluptas aut. Sit repudiandae esse perspiciatis dignissimos error. Itaque quibusdam tempora velit porro ut velit soluta. Eligendi occaecati debitis et saepe. Sint dolorem delectus enim ipsum inventore sed libero. Velit qui suscipit a deserunt laudantium quibusdam enim. Soluta qui ipsam non ipsum. Reiciendis aperiam et fuga doloribus nisi. <iframe src="https://www.facebook.com/plugins/post.php?href=https%3A%2F%2Fwww.facebook.com%2Fniemanlab%2Fposts%2F10154594541763654&width=500" width="500" height="482" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true"></iframe>',
96
                    'route' => 'amp-articles-tenant-2',
97
                    'locale' => 'en',
98
                    'tenant' => '456def',
99
                ],
100
            ];
101
102 3 View Code Duplication
            foreach ($articles as $articleData) {
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...
103 3
                $article = $this->container->get('swp.factory.article')->create();
104 3
                $article->setTitle($articleData['title']);
105 3
                $article->setBody($articleData['content']);
106 3
                $article->setRoute($this->getRouteByName($articleData['route']));
107 3
                $article->setLocale($articleData['locale']);
108 3
                $article->setPublishable(true);
109 3
                $article->setPublishedAt(new \DateTime());
110 3
                $article->setStatus(ArticleInterface::STATUS_PUBLISHED);
111 3
                $article->setTenantCode($articleData['tenant']);
112 3
                $manager->persist($article);
113
114 3
                $this->addReference($article->getSlug(), $article);
115
            }
116
117 3
            $manager->flush();
118
        }
119 3
    }
120
}
121