SocialPostExtension   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 102
Duplicated Lines 39.22 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 40
loc 102
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 14 1
A setFacebookParameters() 0 18 3
A setLinkedInParameters() 20 20 4
A setTwitterParameters() 20 20 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
namespace MartinGeorgiev\SocialPostBundle\DependencyInjection;
6
7
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
8
use Symfony\Component\Config\FileLocator;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\DependencyInjection\Extension\Extension;
11
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
12
13
/**
14
 * @since 1.0.0
15
 * @license https://opensource.org/licenses/MIT
16
 * @link https://github.com/martin-georgiev/social-post-bundle
17
 */
18
class SocialPostExtension extends Extension
19
{
20
    /**
21
     * @var array
22
     */
23
    private $configuration;
24
25
    /**
26
     * @var ContainerBuilder
27
     */
28
    private $container;
29
30
    /**
31
     * @var YamlFileLoader
32
     */
33
    private $loader;
34
35
    public function load(array $configs, ContainerBuilder $container): void
36
    {
37
        $this->container = $container;
38
        $this->loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config/service'));
39
        $this->configuration = $this->processConfiguration(new Configuration(), $configs);
40
41
        $this->container->setParameter('social_post.configuration.publish_on', $this->configuration['publish_on']);
42
43
        $this->setFacebookParameters();
44
        $this->setLinkedInParameters();
45
        $this->setTwitterParameters();
46
47
        $this->loader->load('all_in_one.yml');
48
    }
49
50
    /**
51
     * @throws InvalidConfigurationException
52
     */
53
    private function setFacebookParameters(): void
54
    {
55
        $configuration = $this->configuration;
56
57
        if (!in_array('facebook', $configuration['publish_on'], true)) {
58
            return;
59
        }
60
61
        if (!isset($configuration['providers']['facebook'])) {
62
            throw new InvalidConfigurationException('Found no configuration for the Facebook provider');
63
        }
64
65
        $facebookConfiguration = $configuration['providers']['facebook'];
66
        $this->container->setParameter('social_post.configuration.facebook', $facebookConfiguration);
67
        $this->container->setParameter('social_post.configuration.facebook.page_id', $facebookConfiguration['page_id']);
68
69
        $this->loader->load('facebook.yml');
70
    }
71
72
    /**
73
     * @throws InvalidConfigurationException
74
     */
75 View Code Duplication
    private function setLinkedInParameters(): void
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...
76
    {
77
        $configuration = $this->configuration;
78
79
        if (!in_array('linkedin', $configuration['publish_on'], true)) {
80
            return;
81
        }
82
83
        if (!isset($configuration['providers']['linkedin'])) {
84
            throw new InvalidConfigurationException('Found no configuration for the LinkedIn provider');
85
        }
86
87
        $linkedinConfiguration = $configuration['providers']['linkedin'];
88
        $linkedinParameters = ['client_id', 'client_secret', 'access_token', 'company_page_id'];
89
        foreach ($linkedinParameters as $parameter) {
90
            $this->container->setParameter('social_post.configuration.linkedin.'.$parameter, $linkedinConfiguration[$parameter]);
91
        }
92
93
        $this->loader->load('linkedin.yml');
94
    }
95
96
    /**
97
     * @throws InvalidConfigurationException
98
     */
99 View Code Duplication
    private function setTwitterParameters(): void
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...
100
    {
101
        $configuration = $this->configuration;
102
103
        if (!in_array('twitter', $configuration['publish_on'], true)) {
104
            return;
105
        }
106
107
        if (!isset($configuration['providers']['twitter'])) {
108
            throw new InvalidConfigurationException('Found no configuration for the Twitter provider');
109
        }
110
111
        $twitterConfiguration = $configuration['providers']['twitter'];
112
        $twitterParameters = ['consumer_key', 'consumer_secret', 'access_token', 'access_token_secret'];
113
        foreach ($twitterParameters as $parameter) {
114
            $this->container->setParameter('social_post.configuration.twitter.'.$parameter, $twitterConfiguration[$parameter]);
115
        }
116
117
        $this->loader->load('twitter.yml');
118
    }
119
}
120