]>
Commit | Line | Data |
---|---|---|
7aab0ecf BD |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\GuzzleSiteAuthenticator; | |
4 | ||
5 | use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig; | |
6 | use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfigBuilder; | |
7 | use Graby\SiteConfig\ConfigBuilder; | |
8 | use OutOfRangeException; | |
9 | ||
10 | class GrabySiteConfigBuilder implements SiteConfigBuilder | |
11 | { | |
12 | /** | |
13 | * @var \Graby\SiteConfig\ConfigBuilder | |
14 | */ | |
15 | private $grabyConfigBuilder; | |
16 | /** | |
17 | * @var array | |
18 | */ | |
19 | private $credentials; | |
20 | ||
21 | /** | |
22 | * GrabySiteConfigBuilder constructor. | |
23 | * | |
24 | * @param \Graby\SiteConfig\ConfigBuilder $grabyConfigBuilder | |
25 | * @param array $credentials | |
26 | */ | |
27 | public function __construct(ConfigBuilder $grabyConfigBuilder, array $credentials = []) | |
28 | { | |
29 | $this->grabyConfigBuilder = $grabyConfigBuilder; | |
30 | $this->credentials = $credentials; | |
31 | } | |
32 | ||
33 | /** | |
34 | * Builds the SiteConfig for a host. | |
35 | * | |
36 | * @param string $host The "www." prefix is ignored | |
37 | * | |
38 | * @return SiteConfig | |
39 | * | |
40 | * @throws OutOfRangeException If there is no config for $host | |
41 | */ | |
42 | public function buildForHost($host) | |
43 | { | |
44 | // required by credentials below | |
45 | $host = strtolower($host); | |
46 | if (substr($host, 0, 4) == 'www.') { | |
47 | $host = substr($host, 4); | |
48 | } | |
49 | ||
50 | $config = $this->grabyConfigBuilder->buildForHost($host); | |
51 | $parameters = [ | |
52 | 'host' => $host, | |
53 | 'requiresLogin' => $config->requires_login ?: false, | |
54 | 'loginUri' => $config->login_uri ?: null, | |
55 | 'usernameField' => $config->login_username_field ?: null, | |
56 | 'passwordField' => $config->login_password_field ?: null, | |
662db41b | 57 | 'extraFields' => $this->processExtraFields($config->login_extra_fields), |
7aab0ecf BD |
58 | 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null, |
59 | ]; | |
60 | ||
61 | if (isset($this->credentials[$host])) { | |
62 | $parameters['username'] = $this->credentials[$host]['username']; | |
63 | $parameters['password'] = $this->credentials[$host]['password']; | |
64 | } | |
65 | ||
66 | return new SiteConfig($parameters); | |
67 | } | |
662db41b BD |
68 | |
69 | /** | |
70 | * Processes login_extra_fields config, transforming an '=' separated array of strings | |
71 | * into a key/value array. | |
72 | * | |
73 | * @param array|mixed $extraFieldsStrings | |
74 | * | |
75 | * @return array | |
76 | */ | |
77 | protected function processExtraFields($extraFieldsStrings) | |
78 | { | |
79 | if (!is_array($extraFieldsStrings)) { | |
80 | return []; | |
81 | } | |
82 | ||
83 | $extraFields = []; | |
84 | foreach ($extraFieldsStrings as $extraField) { | |
85 | if (strpos($extraField, '=') === false) { | |
86 | continue; | |
87 | } | |
88 | list($fieldName, $fieldValue) = explode('=', $extraField, 2); | |
89 | $extraFields[$fieldName] = $fieldValue; | |
90 | } | |
91 | ||
92 | return $extraFields; | |
93 | } | |
7aab0ecf | 94 | } |