-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathConfig.php
More file actions
131 lines (104 loc) · 3.01 KB
/
Config.php
File metadata and controls
131 lines (104 loc) · 3.01 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
declare(strict_types=1);
namespace Shapecode\FUT\Client\Config;
use Symfony\Component\OptionsResolver\OptionsResolver;
use const JSON_THROW_ON_ERROR;
use function array_merge;
use function file_get_contents;
use function is_array;
use function json_decode;
use function random_int;
class Config implements ConfigInterface
{
// phpcs:disable Generic.Files.LineLength.TooLong
protected const USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36';
// phpcs:enable Generic.Files.LineLength.TooLong
/** @var string */
protected $futConfigUrl;
/** @var mixed[] */
protected $options;
/**
* @param mixed[] $options
*/
public function __construct(array $options = [], ?string $futConfigUrl = null)
{
if ($futConfigUrl === null) {
$futConfigUrl = 'https://www.easports.com/fifa/ultimate-team/web-app/config/config.json';
}
$this->futConfigUrl = $futConfigUrl;
$this->resolveOptions($options);
}
public function isDelay() : bool
{
return $this->getOption('delay');
}
public function getDelayMinTime() : int
{
return $this->getOption('delayMinTime');
}
public function getDelayMaxTime() : int
{
return $this->getOption('delayMaxTime');
}
public function getRandomDelayTime(?int $min = null, ?int $max = null) : int
{
if ($min === null) {
$min = $this->getDelayMinTime();
}
if ($max === null) {
$max = $this->getDelayMaxTime();
}
$delayMS = random_int($min, $max);
return $delayMS * 1000;
}
public function getUserAgent() : string
{
return $this->getOption('userAgent');
}
/**
* @inheritDoc
*/
public function getOptions() : array
{
return $this->options;
}
/**
* @inheritDoc
*/
public function getOption(string $name)
{
return $this->options[$name];
}
/**
* @inheritDoc
*/
public function setOption(string $name, $value) : void
{
$this->options[$name] = $value;
}
protected function getResolver() : OptionsResolver
{
$resolver = new OptionsResolver();
$url = $this->futConfigUrl;
$content = file_get_contents($url);
$futConfig = $content !== false? json_decode($content, true, 512, JSON_THROW_ON_ERROR) :null;
if (! is_array($futConfig)) {
$futConfig = [];
}
$defaults = array_merge($futConfig, [
'userAgent' => self::USER_AGENT,
'delay' => true,
'delayMinTime' => 1000,
'delayMaxTime' => 1500,
]);
$resolver->setDefaults($defaults);
return $resolver;
}
/**
* @param mixed[] $options
*/
protected function resolveOptions(array $options) : void
{
$this->options = $this->getResolver()->resolve($options);
}
}