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

LoadArticlesMediaData::loadArticles()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 95
Code Lines 68

Duplication

Lines 18
Ratio 18.95 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 18
loc 95
ccs 0
cts 82
cp 0
rs 8.2079
c 0
b 0
f 0
cc 5
eloc 68
nc 5
nop 2
crap 30

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.u. 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\PHPCR;
16
17
use Doctrine\Common\DataFixtures\FixtureInterface;
18
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
19
use Doctrine\Common\Persistence\ObjectManager;
20
use Doctrine\ODM\PHPCR\Document\Generic;
21
use Faker\Provider\Image;
22
use SWP\Bundle\ContentBundle\Doctrine\ODM\PHPCR\Article;
23
use SWP\Bundle\ContentBundle\Doctrine\ODM\PHPCR\ArticleMedia;
24
use SWP\Bundle\ContentBundle\Doctrine\ODM\PHPCR\ImageRendition;
25
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
26
use SWP\Bundle\FixturesBundle\AbstractFixture;
27
use Symfony\Component\HttpFoundation\File\UploadedFile;
28
29
class LoadArticlesMediaData extends AbstractFixture implements FixtureInterface, OrderedFixtureInterface
30
{
31
    private $manager;
32
    private $defaultTenantPrefix;
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 View Code Duplication
    public function load(ObjectManager $manager)
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...
38
    {
39
        $this->manager = $manager;
40
        $env = $this->getEnvironment();
41
42
        $this->defaultTenantPrefix = $this->getTenantPrefix();
43
        $this->loadArticles($env, $manager);
44
45
        $manager->flush();
46
    }
47
48
    /**
49
     * Sets articles manually (not via Alice) for test env due to fatal error:
50
     * Method PHPCRProxies\__CG__\Doctrine\ODM\PHPCR\Document\Generic::__toString() must not throw an exception.
51
     */
52
    public function loadArticles($env, $manager)
53
    {
54
        $articles = [
55
            'test' => [
56
                [
57
                    'title' => 'Test news article',
58
                    'content' => 'Test news article content',
59
                    'parent' => $this->defaultTenantPrefix.'/content',
60
                    'locale' => 'en',
61
                ],
62
            ],
63
        ];
64
65
        $renditions = [
66
            'original' => [
67
                'width' => '4000',
68
                'height' => '2667',
69
                'media' => '12345678987654321a',
70
            ],
71
            '16-9' => [
72
                'width' => '1079',
73
                'height' => '720',
74
                'media' => '12345678987654321b',
75
            ],
76
            '4-3' => [
77
                'width' => '800',
78
                'height' => '533',
79
                'media' => '12345678987654321c',
80
            ],
81
        ];
82
83
        $mediaManager = $this->container->get('swp_content_bundle.manager.media');
84
        $fakeImage = __DIR__.'/../../Resources/assets/test_cc_image.jpg';
85
86
        if (isset($articles[$env])) {
87
            foreach ($articles[$env] as $articleData) {
88
                $article = new Article();
89
                $article->setParent($manager->find(null, $articleData['parent']));
90
                $article->setTitle($articleData['title']);
91
                $article->setBody($articleData['content']);
92
                $article->setLocale($articleData['locale']);
93
                $article->setPublishedAt(new \DateTime());
94
                $article->setPublishable(true);
95
                $article->setStatus(ArticleInterface::STATUS_PUBLISHED);
96
                $manager->persist($article);
97
98
                $mediaDocument = new Generic();
99
                $mediaDocument
100
                    ->setParentDocument($article)
101
                    ->setNodename('media');
102
                $manager->persist($mediaDocument);
103
104
                // create Media
105
                $articleMedia = new ArticleMedia();
106
                $articleMedia->setId('12345678987654321a');
107
                $articleMedia->setParent($mediaDocument);
108
                $articleMedia->setArticle($article);
109
                $articleMedia->setBody('article media body');
110
                $articleMedia->setByLine('By Best Editor');
111
                $articleMedia->setLocated('Porto');
112
                $articleMedia->setDescription('Media description');
113
                $articleMedia->setUsageTerms('Some super open terms');
114
                $articleMedia->setMimetype('image/jpeg');
115
116
                // create media renditions
117
                $renditionsDocument = new Generic();
118
                $renditionsDocument
119
                    ->setParentDocument($articleMedia)
120
                    ->setNodename('renditions');
121
                $manager->persist($renditionsDocument);
122
123
                /* @var $rendition Rendition */
124 View Code Duplication
                foreach ($renditions as $key => $rendition) {
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...
125
                    $uploadedFile = new UploadedFile($fakeImage, $rendition['media'], 'image/jpeg', filesize($fakeImage), null, true);
126
                    $image = $mediaManager->handleUploadedFile($uploadedFile, $rendition['media']);
127
128
                    if ($key === 'original') {
129
                        $articleMedia->setImage($image);
130
                    }
131
132
                    $imageRendition = new ImageRendition();
133
                    $imageRendition->setParent($renditionsDocument);
134
                    $imageRendition->setImage($image);
135
                    $imageRendition->setMedia($articleMedia);
136
                    $imageRendition->setHeight($rendition['height']);
137
                    $imageRendition->setWidth($rendition['width']);
138
                    $imageRendition->setName($key);
139
                    $manager->persist($imageRendition);
140
                    $articleMedia->addRendition($imageRendition);
141
                }
142
            }
143
144
            $manager->flush();
145
        }
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function getOrder()
152
    {
153
        return 999;
154
    }
155
}
156