1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MartinGeorgiev\SocialPost\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
|
|
|
* @author Martin Georgiev <[email protected]> |
16
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
17
|
|
|
* @link https://github.com/martin-georgiev/social-post-bundle Package's homepage |
18
|
|
|
*/ |
19
|
|
|
class SocialPostExtension extends Extension |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
public function load(array $configs, ContainerBuilder $container) |
25
|
|
|
{ |
26
|
|
|
$configuration = $this->processConfiguration(new Configuration(), $configs); |
27
|
|
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
28
|
|
|
$loader->load('services.yml'); |
29
|
|
|
|
30
|
|
|
$container->setParameter('social_post.configuration.publish_on', $configuration['publish_on']); |
31
|
|
|
|
32
|
|
|
$this->setFacebookParameters($configuration, $container); |
33
|
|
|
$this->setLinkedInParameters($configuration, $container); |
34
|
|
|
$this->setTwitterParameters($configuration, $container); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $configuration |
39
|
|
|
* @param ContainerBuilder $container |
40
|
|
|
* @throws InvalidConfigurationException |
41
|
|
|
*/ |
42
|
|
|
private function setFacebookParameters(array $configuration, ContainerBuilder $container) |
43
|
|
|
{ |
44
|
|
|
if (!in_array('facebook', $configuration['publish_on'])) { |
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (!isset($configuration['providers']['facebook'])) { |
49
|
|
|
throw new InvalidConfigurationException('Found no configuration for the Facebook provider'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$facebookConfiguration = $configuration['providers']['facebook']; |
53
|
|
|
$container->setParameter('social_post.configuration.facebook', $facebookConfiguration); |
54
|
|
|
$container->setParameter('social_post.configuration.facebook.page_id', $facebookConfiguration['page_id']); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param array $configuration |
59
|
|
|
* @param ContainerBuilder $container |
60
|
|
|
* @throws InvalidConfigurationException |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
private function setLinkedInParameters(array $configuration, ContainerBuilder $container) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
if (!in_array('linkedin', $configuration['publish_on'])) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (!isset($configuration['providers']['linkedin'])) { |
69
|
|
|
throw new InvalidConfigurationException('Found no configuration for the LinkedIn provider'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$linkedinConfiguration = $configuration['providers']['linkedin']; |
73
|
|
|
$linkedinParameters = ['client_id', 'client_secret', 'company_page_id']; |
74
|
|
|
foreach ($linkedinParameters as $parameter) { |
75
|
|
|
$container->setParameter('social_post.configuration.linkedin.' . $parameter, $linkedinConfiguration[$parameter]); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array $configuration |
81
|
|
|
* @param ContainerBuilder $container |
82
|
|
|
* @throws InvalidConfigurationException |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
private function setTwitterParameters(array $configuration, ContainerBuilder $container) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
if (!in_array('twitter', $configuration['publish_on'])) { |
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (!isset($configuration['providers']['twitter'])) { |
91
|
|
|
throw new InvalidConfigurationException('Found no configuration for the Twitter provider'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$twitterConfiguration = $configuration['providers']['twitter']; |
95
|
|
|
$twitterParameters = ['consumer_key', 'consumer_secret', 'access_token', 'access_token_secret']; |
96
|
|
|
foreach ($twitterParameters as $parameter) { |
97
|
|
|
$container->setParameter('social_post.configuration.twitter.' . $parameter, $twitterConfiguration[$parameter]); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.