GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 1e6437...ac5983 )
by Odiseo
10:05
created

BlogFixture::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 13
c 1
b 0
f 1
dl 0
loc 24
rs 9.8333
cc 1
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Odiseo\SyliusBlogPlugin\Fixture;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
use Faker\Factory;
7
use Odiseo\BlogBundle\EventListener\ArticleImageUploadListener;
8
use Odiseo\BlogBundle\Model\ArticleCategoryInterface;
9
use Odiseo\BlogBundle\Model\ArticleImage;
10
use Odiseo\BlogBundle\Model\ArticleImageInterface;
11
use Odiseo\SyliusBlogPlugin\Model\ArticleCommentInterface;
12
use Odiseo\SyliusBlogPlugin\Model\ArticleInterface;
13
use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture;
14
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
15
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
16
use Sylius\Component\Core\Model\ChannelInterface;
17
use Sylius\Component\Locale\Model\LocaleInterface;
18
use Sylius\Component\Resource\Factory\FactoryInterface;
19
use Sylius\Component\Resource\Repository\RepositoryInterface;
20
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
21
use Symfony\Component\HttpFoundation\File\UploadedFile;
22
use Symfony\Component\OptionsResolver\OptionsResolver;
23
24
class BlogFixture extends AbstractFixture
25
{
26
    /** @var ObjectManager */
27
    protected $objectManager;
28
29
    /** @var FactoryInterface */
30
    protected $articleFactory;
31
32
    /** @var FactoryInterface */
33
    protected $articleCategoryFactory;
34
35
    /** @var FactoryInterface */
36
    protected $articleImageFactory;
37
38
    /** @var FactoryInterface */
39
    protected $articleCommentFactory;
40
41
    /** @var ChannelRepositoryInterface */
42
    protected $channelRepository;
43
44
    /** @var RepositoryInterface */
45
    protected $localeRepository;
46
47
    /** @var \Faker\Generator */
48
    protected $faker;
49
50
    /** @var OptionsResolver */
51
    protected $optionsResolver;
52
53
    /** @var ArticleImageUploadListener */
54
    protected $imageUploader;
55
56
    /** @var array */
57
    protected $categories;
58
59
    public function __construct(
60
        ObjectManager $objectManager,
61
        FactoryInterface $articleFactory,
62
        FactoryInterface $articleCategoryFactory,
63
        FactoryInterface $articleImageFactory,
64
        FactoryInterface $articleCommentFactory,
65
        ChannelRepositoryInterface $channelRepository,
66
        RepositoryInterface $localeRepository,
67
        ArticleImageUploadListener $imageUploader
68
    ) {
69
        $this->objectManager = $objectManager;
70
        $this->articleFactory = $articleFactory;
71
        $this->articleCategoryFactory = $articleCategoryFactory;
72
        $this->articleImageFactory = $articleImageFactory;
73
        $this->articleCommentFactory = $articleCommentFactory;
74
        $this->channelRepository = $channelRepository;
75
        $this->localeRepository = $localeRepository;
76
        $this->imageUploader = $imageUploader;
77
78
        $this->faker = Factory::create();
79
        $this->optionsResolver =
80
            (new OptionsResolver())
81
                ->setRequired('articles_per_channel')
82
                ->setAllowedTypes('articles_per_channel', 'int')
83
        ;
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89
    public function load(array $options): void
90
    {
91
        $options = $this->optionsResolver->resolve($options);
92
93
        $channels = $this->channelRepository->findAll();
94
95
        $this->createArticleCategories();
96
97
        /** @var ChannelInterface $channel */
98
        foreach ($channels as $channel) {
99
            $imageIndex = 1;
0 ignored issues
show
Unused Code introduced by
The assignment to $imageIndex is dead and can be removed.
Loading history...
100
            for ($i=1; $i <= $options['articles_per_channel']; $i++) {
101
                /** @var ArticleInterface $article */
102
                $article = $this->articleFactory->createNew();
103
104
                $article->addChannel($channel);
105
                $article->setCode($this->faker->md5);
106
                $article->setEnabled(rand(1, 100) > 30);
107
108
                // Add categories
109
                $categories = $this->faker->randomElements($this->categories, rand(1, count($this->categories)));
110
111
                /** @var ArticleCategoryInterface $category */
112
                foreach ($categories as $category) {
113
                    $article->addCategory($category);
114
                }
115
116
                // Set translatable fields
117
                foreach ($this->getLocales() as $localeCode) {
118
                    $article->setCurrentLocale($localeCode);
119
                    $article->setFallbackLocale($localeCode);
120
121
                    $article->getTranslation()->setTitle($this->faker->text(20));
0 ignored issues
show
Bug introduced by
The method setTitle() does not exist on Sylius\Component\Resourc...el\TranslationInterface. It seems like you code against a sub-type of Sylius\Component\Resourc...el\TranslationInterface such as Odiseo\BlogBundle\Model\...oryTranslationInterface or Odiseo\BlogBundle\Model\...cleTranslationInterface or Odiseo\BlogBundle\Model\ArticleTranslation or Odiseo\BlogBundle\Model\ArticleCategoryTranslation or AppBundle\Entity\BookTranslation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
                    $article->getTranslation()->/** @scrutinizer ignore-call */ setTitle($this->faker->text(20));
Loading history...
122
                    $article->getTranslation()->setContent($this->faker->text);
0 ignored issues
show
Bug introduced by
The method setContent() does not exist on Sylius\Component\Resourc...el\TranslationInterface. It seems like you code against a sub-type of Sylius\Component\Resourc...el\TranslationInterface such as Odiseo\BlogBundle\Model\...cleTranslationInterface or Odiseo\BlogBundle\Model\ArticleTranslation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

122
                    $article->getTranslation()->/** @scrutinizer ignore-call */ setContent($this->faker->text);
Loading history...
123
                    $article->getTranslation()->setSlug($this->faker->slug);
0 ignored issues
show
Bug introduced by
The method setSlug() does not exist on Sylius\Component\Resourc...el\TranslationInterface. It seems like you code against a sub-type of Sylius\Component\Resourc...el\TranslationInterface such as Sylius\Component\Product...uctTranslationInterface or Odiseo\BlogBundle\Model\...oryTranslationInterface or Sylius\Component\Taxonom...xonTranslationInterface or Odiseo\BlogBundle\Model\...cleTranslationInterface or Sylius\Component\Taxonomy\Model\TaxonTranslation or Odiseo\BlogBundle\Model\ArticleTranslation or Odiseo\BlogBundle\Model\ArticleCategoryTranslation or Sylius\Component\Product\Model\ProductTranslation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

123
                    $article->getTranslation()->/** @scrutinizer ignore-call */ setSlug($this->faker->slug);
Loading history...
124
                }
125
126
                // Add images
127
                $srcImage = new UploadedFile($this->faker->image(null, 1200, 350), $article->getTitle());
128
                /** @var ArticleImageInterface $image */
129
                $image = $this->articleImageFactory->createNew();
130
                $image->setFile($srcImage);
131
                $article->addImage($image);
132
133
                $this->imageUploader->uploadImages(new ResourceControllerEvent($article));
134
135
                // Add comments
136
                if (rand(0, 100) > 50) {
137
                    $this->addArticleComments($article);
138
                }
139
140
                $this->objectManager->persist($article);
141
            }
142
        }
143
144
        $this->objectManager->flush();
145
    }
146
147
    private function createArticleCategories()
148
    {
149
        for ($i = 0; $i < 5; $i++) {
150
            /** @var ArticleCategoryInterface $articleCategory */
151
            $articleCategory = $this->articleCategoryFactory->createNew();
152
            $articleCategory->setCode($this->faker->md5);
153
            $articleCategory->setEnabled(true);
154
155
            foreach ($this->getLocales() as $localeCode) {
156
                $articleCategory->setCurrentLocale($localeCode);
157
                $articleCategory->setFallbackLocale($localeCode);
158
159
                $articleCategory->getTranslation()->setTitle($this->faker->text(20));
160
                $articleCategory->getTranslation()->setSlug($this->faker->slug);
161
            }
162
163
            $this->objectManager->persist($articleCategory);
164
            $this->categories[] = $articleCategory;
165
        }
166
    }
167
168
    private function addArticleComments(ArticleInterface $article)
169
    {
170
        for ($i = 0; $i < 8; $i++) {
171
            /** @var ArticleCommentInterface $comment */
172
            $comment = $this->articleCommentFactory->createNew();
173
            $comment->setName($this->faker->name);
174
            $comment->setEmail($this->faker->email);
175
            $comment->setComment($this->faker->text);
176
            $comment->setEnabled(rand(1, 100) > 30);
177
178
            $article->addComment($comment);
179
            $this->objectManager->persist($comment);
180
        }
181
    }
182
183
    /**
184
     * @return array
185
     */
186
    private function getLocales()
187
    {
188
        /** @var LocaleInterface[] $locales */
189
        $locales = $this->localeRepository->findAll();
190
        foreach ($locales as $locale) {
191
            yield $locale->getCode();
0 ignored issues
show
Bug Best Practice introduced by
The expression yield $locale->getCode() returns the type Generator which is incompatible with the documented return type array.
Loading history...
192
        }
193
    }
194
195
    /**
196
     * {@inheritdoc}
197
     */
198
    protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
199
    {
200
        $optionsNode
201
            ->children()
202
                ->integerNode('articles_per_channel')->isRequired()->min(1)->end()
203
        ;
204
    }
205
206
    /**
207
     * {@inheritdoc}
208
     */
209
    public function getName(): string
210
    {
211
        return 'blog';
212
    }
213
}
214