]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
Fix some Scrutinizer issues
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / GuzzleSiteAuthenticator / GrabySiteConfigBuilder.php
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 use Psr\Log\LoggerInterface;
10
11 class GrabySiteConfigBuilder implements SiteConfigBuilder
12 {
13 /**
14 * @var ConfigBuilder
15 */
16 private $grabyConfigBuilder;
17 /**
18 * @var array
19 */
20 private $credentials;
21 /**
22 * @var LoggerInterface
23 */
24 private $logger;
25
26 /**
27 * GrabySiteConfigBuilder constructor.
28 *
29 * @param ConfigBuilder $grabyConfigBuilder
30 * @param array $credentials
31 * @param LoggerInterface $logger
32 */
33 public function __construct(ConfigBuilder $grabyConfigBuilder, array $credentials, LoggerInterface $logger)
34 {
35 $this->grabyConfigBuilder = $grabyConfigBuilder;
36 $this->credentials = $credentials;
37 $this->logger = $logger;
38 }
39
40 /**
41 * {@inheritdoc}
42 */
43 public function buildForHost($host)
44 {
45 // required by credentials below
46 $host = strtolower($host);
47 if (substr($host, 0, 4) == 'www.') {
48 $host = substr($host, 4);
49 }
50
51 if (empty($this->credentials[$host])) {
52 $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]);
53
54 return false;
55 }
56
57 $config = $this->grabyConfigBuilder->buildForHost($host);
58 $parameters = [
59 'host' => $host,
60 'requiresLogin' => $config->requires_login ?: false,
61 'loginUri' => $config->login_uri ?: null,
62 'usernameField' => $config->login_username_field ?: null,
63 'passwordField' => $config->login_password_field ?: null,
64 'extraFields' => $this->processExtraFields($config->login_extra_fields),
65 'notLoggedInXpath' => $config->not_logged_in_xpath ?: null,
66 'username' => $this->credentials[$host]['username'],
67 'password' => $this->credentials[$host]['password'],
68 ];
69
70 $config = new SiteConfig($parameters);
71
72 // do not leak password in log
73 $parameters['password'] = '**masked**';
74
75 $this->logger->debug('Auth: add parameters.', ['host' => $host, 'parameters' => $parameters]);
76
77 return $config;
78 }
79
80 /**
81 * Processes login_extra_fields config, transforming an '=' separated array of strings
82 * into a key/value array.
83 *
84 * @param array|mixed $extraFieldsStrings
85 *
86 * @return array
87 */
88 protected function processExtraFields($extraFieldsStrings)
89 {
90 if (!is_array($extraFieldsStrings)) {
91 return [];
92 }
93
94 $extraFields = [];
95 foreach ($extraFieldsStrings as $extraField) {
96 if (strpos($extraField, '=') === false) {
97 continue;
98 }
99
100 list($fieldName, $fieldValue) = explode('=', $extraField, 2);
101 $extraFields[$fieldName] = $fieldValue;
102 }
103
104 return $extraFields;
105 }
106 }