Completed
Push — master ( 6469d9...c2139f )
by Paweł
61:46
created

LoadArticlesWithMetadata::load()   C

Complexity

Conditions 7
Paths 19

Size

Total Lines 90
Code Lines 58

Duplication

Lines 21
Ratio 23.33 %

Importance

Changes 0
Metric Value
dl 21
loc 90
rs 6.5083
c 0
b 0
f 0
cc 7
eloc 58
nc 19
nop 1

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\FixturesBundle\AbstractFixture;
8
9
class LoadArticlesWithMetadata extends AbstractFixture implements FixtureInterface
10
{
11
    private $manager;
12
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function load(ObjectManager $manager)
17
    {
18
        $this->manager = $manager;
19
        $env = $this->getEnvironment();
20
21
        if ($env === 'test') {
22
            $routes = [
23
                [
24
                    'name' => 'news',
25
                    'variablePattern' => '/{slug}',
26
                    'requirements' => [
27
                        'slug' => '[a-zA-Z0-9*\-_\/]+',
28
                    ],
29
                    'type' => 'collection',
30
                    'defaults' => [
31
                        'slug' => null,
32
                    ],
33
                    'templateName' => 'articles_by_metadata_v1.html.twig',
34
                    'articlesTemplateName' => 'article.html.twig',
35
                    'tenant' => '123abc',
36
                ],
37
            ];
38
39
            $routeService = $this->container->get('swp.service.route');
40
41 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...
42
                $route = $this->container->get('swp.factory.route')->create();
43
                $route->setName($routeData['name']);
44
                $route->setType($routeData['type']);
45
46
                if (isset($routeData['templateName'])) {
47
                    $route->setTemplateName($routeData['templateName']);
48
                }
49
50
                if (isset($routeData['articlesTemplateName'])) {
51
                    $route->setArticlesTemplateName($routeData['articlesTemplateName']);
52
                }
53
54
                if (isset($routeData['tenant'])) {
55
                    $route->setTenantCode($routeData['tenant']);
56
                }
57
58
                $route = $routeService->fillRoute($route);
59
60
                $manager->persist($route);
61
            }
62
63
            $manager->flush();
64
65
            $articles = [
66
                [
67
                    'title' => 'Article 1',
68
                    'content' => 'test content 1',
69
                    'route' => 'news',
70
                    'locale' => 'en',
71
                    'tenant' => '123abc',
72
                    'author' => 'Test Persona',
73
                ],
74
                [
75
                    'title' => 'Article 2',
76
                    'content' => 'test content 2',
77
                    'route' => 'news',
78
                    'locale' => 'en',
79
                    'tenant' => '123abc',
80
                    'author' => 'Karen Ruhiger',
81
                ],
82
            ];
83
84
            $articleService = $this->container->get('swp.service.article');
85
            foreach ($articles as $articleData) {
86
                $article = $this->container->get('swp.factory.article')->create();
87
                $article->setTitle($articleData['title']);
88
                $article->setBody($articleData['content']);
89
                $article->setRoute($this->getRouteByName($articleData['route']));
90
                $article->setLocale($articleData['locale']);
91
                $article->setCode(md5($articleData['title']));
92
                $article->setMetadata([
93
                    'located' => 'Sydney',
94
                    'byline' => $articleData['author'],
95
                ]);
96
                $manager->persist($article);
97
                $articleService->publish($article);
98
                $article->setTenantCode($articleData['tenant']);
99
100
                $this->addReference($article->getSlug(), $article);
101
            }
102
103
            $manager->flush();
104
        }
105
    }
106
}
107