|
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
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
private $configuration; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var ContainerBuilder |
|
28
|
|
|
*/ |
|
29
|
|
|
private $container; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var YamlFileLoader |
|
33
|
|
|
*/ |
|
34
|
|
|
private $loader; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
public function load(array $configs, ContainerBuilder $container) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->container = $container; |
|
42
|
|
|
$this->loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config/service')); |
|
43
|
|
|
$this->configuration = $this->processConfiguration(new Configuration(), $configs); |
|
44
|
|
|
|
|
45
|
|
|
$this->container->setParameter('social_post.configuration.publish_on', $this->configuration['publish_on']); |
|
46
|
|
|
|
|
47
|
|
|
$this->setFacebookParameters(); |
|
48
|
|
|
$this->setLinkedInParameters(); |
|
49
|
|
|
$this->setTwitterParameters(); |
|
50
|
|
|
|
|
51
|
|
|
$this->loader->load('all_in_one.yml'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @throws InvalidConfigurationException |
|
56
|
|
|
*/ |
|
57
|
|
|
private function setFacebookParameters() |
|
58
|
|
|
{ |
|
59
|
|
|
$configuration = $this->configuration; |
|
60
|
|
|
|
|
61
|
|
|
if (!in_array('facebook', $configuration['publish_on'])) { |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (!isset($configuration['providers']['facebook'])) { |
|
66
|
|
|
throw new InvalidConfigurationException('Found no configuration for the Facebook provider'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$facebookConfiguration = $configuration['providers']['facebook']; |
|
70
|
|
|
$this->container->setParameter('social_post.configuration.facebook', $facebookConfiguration); |
|
71
|
|
|
$this->container->setParameter('social_post.configuration.facebook.page_id', $facebookConfiguration['page_id']); |
|
72
|
|
|
|
|
73
|
|
|
$this->loader->load('facebook.yml'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @throws InvalidConfigurationException |
|
78
|
|
|
*/ |
|
79
|
|
View Code Duplication |
private function setLinkedInParameters() |
|
80
|
|
|
{ |
|
81
|
|
|
$configuration = $this->configuration; |
|
82
|
|
|
|
|
83
|
|
|
if (!in_array('linkedin', $configuration['publish_on'])) { |
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if (!isset($configuration['providers']['linkedin'])) { |
|
88
|
|
|
throw new InvalidConfigurationException('Found no configuration for the LinkedIn provider'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$linkedinConfiguration = $configuration['providers']['linkedin']; |
|
92
|
|
|
$linkedinParameters = ['client_id', 'client_secret', 'access_token', 'company_page_id']; |
|
93
|
|
|
foreach ($linkedinParameters as $parameter) { |
|
94
|
|
|
$this->container->setParameter('social_post.configuration.linkedin.' . $parameter, $linkedinConfiguration[$parameter]); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$this->loader->load('linkedin.yml'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @throws InvalidConfigurationException |
|
102
|
|
|
*/ |
|
103
|
|
View Code Duplication |
private function setTwitterParameters() |
|
104
|
|
|
{ |
|
105
|
|
|
$configuration = $this->configuration; |
|
106
|
|
|
|
|
107
|
|
|
if (!in_array('twitter', $configuration['publish_on'])) { |
|
108
|
|
|
return; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
if (!isset($configuration['providers']['twitter'])) { |
|
112
|
|
|
throw new InvalidConfigurationException('Found no configuration for the Twitter provider'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$twitterConfiguration = $configuration['providers']['twitter']; |
|
116
|
|
|
$twitterParameters = ['consumer_key', 'consumer_secret', 'access_token', 'access_token_secret']; |
|
117
|
|
|
foreach ($twitterParameters as $parameter) { |
|
118
|
|
|
$this->container->setParameter('social_post.configuration.twitter.' . $parameter, $twitterConfiguration[$parameter]); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$this->loader->load('twitter.yml'); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|