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 |
|
|
|
|
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 |
|
|
|
|
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
|
|
|
|
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.