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.
Passed
Push — master ( d376a5...e682e0 )
by
unknown
04:24
created

BlogExampleFactory::getLocales()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Odiseo\SyliusBlogPlugin\Fixture\Factory;
6
7
use Doctrine\Common\Persistence\ObjectManager;
8
use Faker\Factory;
9
use Generator;
10
use Odiseo\BlogBundle\EventListener\ArticleImageUploadListener;
11
use Odiseo\BlogBundle\Model\ArticleCategoryInterface;
12
use Odiseo\BlogBundle\Model\ArticleImageInterface;
13
use Odiseo\SyliusBlogPlugin\Entity\ArticleCommentInterface;
14
use Odiseo\SyliusBlogPlugin\Entity\ArticleInterface;
15
use Sylius\Bundle\CoreBundle\Fixture\Factory\AbstractExampleFactory;
16
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption;
17
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
18
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
19
use Sylius\Component\Locale\Model\LocaleInterface;
20
use Sylius\Component\Resource\Factory\FactoryInterface;
21
use Sylius\Component\Resource\Repository\RepositoryInterface;
22
use Symfony\Component\Config\FileLocatorInterface;
23
use Symfony\Component\HttpFoundation\File\UploadedFile;
24
use Symfony\Component\OptionsResolver\Options;
25
use Symfony\Component\OptionsResolver\OptionsResolver;
26
27
final class BlogExampleFactory extends AbstractExampleFactory
28
{
29
    /** @var ObjectManager */
30
    private $objectManager;
31
32
    /** @var FactoryInterface */
33
    private $articleFactory;
34
35
    /** @var FactoryInterface */
36
    private $articleCategoryFactory;
37
38
    /** @var FactoryInterface */
39
    private $articleImageFactory;
40
41
    /** @var FactoryInterface */
42
    private $articleCommentFactory;
43
44
    /** @var ChannelRepositoryInterface */
45
    private $channelRepository;
46
47
    /** @var RepositoryInterface */
48
    private $localeRepository;
49
50
    /** @var ArticleImageUploadListener */
51
    private $imageUploader;
52
53
    /** @var FileLocatorInterface|null */
54
    private $fileLocator;
55
56
    /** @var OptionsResolver */
57
    private $optionsResolver;
58
59
    /** @var \Faker\Generator */
60
    private $faker;
61
62
    public function __construct(
63
        ObjectManager $objectManager,
64
        FactoryInterface $articleFactory,
65
        FactoryInterface $articleCategoryFactory,
66
        FactoryInterface $articleImageFactory,
67
        FactoryInterface $articleCommentFactory,
68
        ChannelRepositoryInterface $channelRepository,
69
        RepositoryInterface $localeRepository,
70
        ArticleImageUploadListener $imageUploader,
71
        ?FileLocatorInterface $fileLocator = null
72
    ) {
73
        $this->objectManager = $objectManager;
74
        $this->articleFactory = $articleFactory;
75
        $this->articleCategoryFactory = $articleCategoryFactory;
76
        $this->articleImageFactory = $articleImageFactory;
77
        $this->articleCommentFactory = $articleCommentFactory;
78
        $this->channelRepository = $channelRepository;
79
        $this->localeRepository = $localeRepository;
80
        $this->imageUploader = $imageUploader;
81
        $this->fileLocator = $fileLocator;
82
83
        $this->faker = Factory::create();
84
        $this->optionsResolver = new OptionsResolver();
85
86
        $this->configureOptions($this->optionsResolver);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    protected function configureOptions(OptionsResolver $resolver): void
93
    {
94
        $resolver
95
            ->setDefault('channels', LazyOption::randomOnes($this->channelRepository, 3))
96
            ->setAllowedTypes('channels', 'array')
97
            ->setNormalizer('channels', LazyOption::findBy($this->channelRepository, 'code'))
98
99
            ->setDefault('image', function (Options $options): string {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

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

99
            ->setDefault('image', function (/** @scrutinizer ignore-unused */ Options $options): string {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
                return __DIR__.'/../../Resources/fixtures/article/0'.rand(1, 4).'.png';
101
            })
102
            ->setAllowedTypes('image', ['string'])
103
        ;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function create(array $options = []): ArticleInterface
110
    {
111
        $options = $this->optionsResolver->resolve($options);
112
113
        /** @var ArticleInterface $article */
114
        $article = $this->articleFactory->createNew();
115
        $article->setCode($this->faker->md5);
116
        $article->setEnabled(rand(1, 100) > 30);
117
118
        foreach ($options['channels'] as $channel) {
119
            $article->addChannel($channel);
120
        }
121
122
        $this->createArticleCategory($article);
123
124
        /** @var string $localeCode */
125
        foreach ($this->getLocales() as $localeCode) {
126
            $article->setCurrentLocale($localeCode);
127
            $article->setFallbackLocale($localeCode);
128
129
            $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

129
            $article->getTranslation()->/** @scrutinizer ignore-call */ setTitle($this->faker->text(20));
Loading history...
130
            $article->getTranslation()->setContent($this->faker->text(400));
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

130
            $article->getTranslation()->/** @scrutinizer ignore-call */ setContent($this->faker->text(400));
Loading history...
131
            $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

131
            $article->getTranslation()->/** @scrutinizer ignore-call */ setSlug($this->faker->slug);
Loading history...
132
        }
133
134
        /** @var ArticleImageInterface $image */
135
        $image = $this->articleImageFactory->createNew();
136
        $image->setFile($this->createImage($options['image']));
137
138
        $article->addImage($image);
139
140
        $this->imageUploader->uploadImages(new ResourceControllerEvent($article));
141
142
        if (rand(0, 100) > 50) {
143
            $this->addArticleComments($article);
144
        }
145
146
        return $article;
147
    }
148
149
    /**
150
     * @param ArticleInterface $article
151
     */
152
    private function createArticleCategory(ArticleInterface $article): void
153
    {
154
        /** @var ArticleCategoryInterface $articleCategory */
155
        $articleCategory = $this->articleCategoryFactory->createNew();
156
        $articleCategory->setCode($this->faker->md5);
157
        $articleCategory->setEnabled(true);
158
159
        foreach ($this->getLocales() as $localeCode) {
160
            $articleCategory->setCurrentLocale($localeCode);
161
            $articleCategory->setFallbackLocale($localeCode);
162
163
            $articleCategory->getTranslation()->setTitle($this->faker->text(20));
164
            $articleCategory->getTranslation()->setSlug($this->faker->slug);
165
        }
166
167
        $article->addCategory($articleCategory);
168
169
        $this->objectManager->persist($articleCategory);
170
    }
171
172
    /**
173
     * @param ArticleInterface $article
174
     */
175
    private function addArticleComments(ArticleInterface $article): void
176
    {
177
        for ($i = 0; $i < 8; $i++) {
178
            /** @var ArticleCommentInterface $comment */
179
            $comment = $this->articleCommentFactory->createNew();
180
            $comment->setName($this->faker->name);
181
            $comment->setEmail($this->faker->email);
182
            $comment->setComment($this->faker->text);
183
            $comment->setEnabled(rand(1, 100) > 30);
184
185
            $article->addComment($comment);
186
187
            $this->objectManager->persist($comment);
188
        }
189
    }
190
191
    /**
192
     * @param string $imagePath
193
     * @return UploadedFile
194
     */
195
    private function createImage(string $imagePath): UploadedFile
196
    {
197
        $imagePath = $this->fileLocator === null ? $imagePath : $this->fileLocator->locate($imagePath);
198
        if(is_array($imagePath) && count($imagePath) > 0)
199
            $imagePath = $imagePath[0];
200
201
        return new UploadedFile($imagePath, basename($imagePath));
202
    }
203
204
    /**
205
     * @return Generator
206
     */
207
    private function getLocales(): Generator
208
    {
209
        /** @var LocaleInterface[] $locales */
210
        $locales = $this->localeRepository->findAll();
211
        foreach ($locales as $locale) {
212
            yield $locale->getCode();
213
        }
214
    }
215
}
216