Completed
Push — master ( e48710...5fe26c )
by Rafał
40:26 queued 26:25
created

SeoImageUploader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 53.33 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 24
loc 45
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handleUpload() 24 29 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2019 Sourcefabric z.u. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2019 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\Service;
18
19
use SWP\Bundle\ContentBundle\Manager\MediaManagerInterface;
20
use SWP\Bundle\ContentBundle\Model\ArticleSeoMediaInterface;
21
use SWP\Bundle\ContentBundle\Model\ArticleSeoMetadataInterface;
22
use SWP\Component\Common\Generator\GeneratorInterface;
23
use SWP\Component\Storage\Factory\FactoryInterface;
24
25
class SeoImageUploader implements SeoImageUploaderInterface
26
{
27
    private $randomStringGenerator;
28
29
    private $mediaManager;
30
31
    private $factory;
32
33
    public function __construct(GeneratorInterface $randomStringGenerator, MediaManagerInterface $mediaManager, FactoryInterface $factory)
34
    {
35
        $this->randomStringGenerator = $randomStringGenerator;
36
        $this->mediaManager = $mediaManager;
37
        $this->factory = $factory;
38
    }
39
40
    public function handleUpload(ArticleSeoMetadataInterface $seoMetadata): void
41
    {
42 View Code Duplication
        if (null !== ($file = $seoMetadata->getMetaMediaFile())) {
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...
43
            $image = $this->mediaManager->handleUploadedFile($file, $this->randomStringGenerator->generate(15));
44
            $seoImageMedia = $this->factory->create();
45
            $seoImageMedia->setKey(ArticleSeoMediaInterface::MEDIA_META_KEY);
46
            $seoImageMedia->setImage($image);
47
48
            $seoMetadata->setMetaMedia($seoImageMedia);
0 ignored issues
show
Bug introduced by
The method setMetaMedia() does not exist on SWP\Bundle\ContentBundle...cleSeoMetadataInterface. Did you maybe mean setMetaMediaFile()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
49
        }
50
51 View Code Duplication
        if (null !== ($file = $seoMetadata->getOgMediaFile())) {
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...
52
            $image = $this->mediaManager->handleUploadedFile($file, $this->randomStringGenerator->generate(15));
53
            $seoImageMedia = $this->factory->create();
54
            $seoImageMedia->setKey(ArticleSeoMediaInterface::MEDIA_OG_KEY);
55
            $seoImageMedia->setImage($image);
56
57
            $seoMetadata->setOgMedia($seoImageMedia);
0 ignored issues
show
Bug introduced by
The method setOgMedia() does not exist on SWP\Bundle\ContentBundle...cleSeoMetadataInterface. Did you maybe mean setOgMediaFile()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
58
        }
59
60 View Code Duplication
        if (null !== ($file = $seoMetadata->getTwitterMediaFile())) {
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...
61
            $image = $this->mediaManager->handleUploadedFile($file, $this->randomStringGenerator->generate(15));
62
            $seoImageMedia = $this->factory->create();
63
            $seoImageMedia->setKey(ArticleSeoMediaInterface::MEDIA_TWITTER_KEY);
64
            $seoImageMedia->setImage($image);
65
66
            $seoMetadata->setTwitterMedia($seoImageMedia);
0 ignored issues
show
Bug introduced by
The method setTwitterMedia() does not exist on SWP\Bundle\ContentBundle...cleSeoMetadataInterface. Did you maybe mean setTwitterMediaFile()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
67
        }
68
    }
69
}
70