blob: 6d4129e8063216ecd3ee97981bd43df3c047ad41 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
<?php
namespace Wallabag\CoreBundle\GuzzleSiteAuthenticator;
use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig;
use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfigBuilder;
use Graby\SiteConfig\ConfigBuilder;
use OutOfRangeException;
class GrabySiteConfigBuilder implements SiteConfigBuilder
{
/**
* @var \Graby\SiteConfig\ConfigBuilder
*/
private $grabyConfigBuilder;
/**
* @var array
*/
private $credentials;
/**
* GrabySiteConfigBuilder constructor.
*
* @param \Graby\SiteConfig\ConfigBuilder $grabyConfigBuilder
* @param array $credentials
*/
public function __construct(ConfigBuilder $grabyConfigBuilder, array $credentials = [])
{
$this->grabyConfigBuilder = $grabyConfigBuilder;
$this->credentials = $credentials;
}
/**
* Builds the SiteConfig for a host.
*
* @param string $host The "www." prefix is ignored
*
* @return SiteConfig
*
* @throws OutOfRangeException If there is no config for $host
*/
public function buildForHost($host)
{
// required by credentials below
$host = strtolower($host);
if (substr($host, 0, 4) == 'www.') {
$host = substr($host, 4);
}
$config = $this->grabyConfigBuilder->buildForHost($host);
$parameters = [
'host' => $host,
'requiresLogin' => $config->requires_login ?: false,
'loginUri' => $config->login_uri ?: null,
'usernameField' => $config->login_username_field ?: null,
'passwordField' => $config->login_password_field ?: null,
'extraFields' => is_array($config->login_extra_fields) ? $config->login_extra_fields : [],
'notLoggedInXpath' => $config->not_logged_in_xpath ?: null,
];
if (isset($this->credentials[$host])) {
$parameters['username'] = $this->credentials[$host]['username'];
$parameters['password'] = $this->credentials[$host]['password'];
}
return new SiteConfig($parameters);
}
}
|