3 namespace Wallabag\CoreBundle\GuzzleSiteAuthenticator
;
5 use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig
;
6 use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfigBuilder
;
7 use Graby\SiteConfig\ConfigBuilder
;
8 use OutOfRangeException
;
10 class GrabySiteConfigBuilder
implements SiteConfigBuilder
13 * @var \Graby\SiteConfig\ConfigBuilder
15 private $grabyConfigBuilder;
22 * GrabySiteConfigBuilder constructor.
24 * @param \Graby\SiteConfig\ConfigBuilder $grabyConfigBuilder
25 * @param array $credentials
27 public function __construct(ConfigBuilder
$grabyConfigBuilder, array $credentials = [])
29 $this->grabyConfigBuilder
= $grabyConfigBuilder;
30 $this->credentials
= $credentials;
34 * Builds the SiteConfig for a host.
36 * @param string $host The "www." prefix is ignored
40 * @throws OutOfRangeException If there is no config for $host
42 public function buildForHost($host)
44 // required by credentials below
45 $host = strtolower($host);
46 if (substr($host, 0, 4) == 'www.') {
47 $host = substr($host, 4);
50 $config = $this->grabyConfigBuilder
->buildForHost($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,
57 'extraFields' => $this->processExtraFields($config->login_extra_fields
),
58 'notLoggedInXpath' => $config->not_logged_in_xpath
?: null,
61 if (isset($this->credentials
[$host])) {
62 $parameters['username'] = $this->credentials
[$host]['username'];
63 $parameters['password'] = $this->credentials
[$host]['password'];
66 return new SiteConfig($parameters);
70 * Processes login_extra_fields config, transforming an '=' separated array of strings
71 * into a key/value array.
73 * @param array|mixed $extraFieldsStrings
77 protected function processExtraFields($extraFieldsStrings)
79 if (!is_array($extraFieldsStrings)) {
84 foreach ($extraFieldsStrings as $extraField) {
85 if (strpos($extraField, '=') === false) {
88 list($fieldName, $fieldValue) = explode('=', $extraField, 2);
89 $extraFields[$fieldName] = $fieldValue;