1 | <?php |
||
2 | |||
3 | namespace Mautic\CoreBundle\IpLookup\DoNotSellList; |
||
4 | |||
5 | use Mautic\CoreBundle\Exception\BadConfigurationException; |
||
6 | use Mautic\CoreBundle\Exception\FileNotFoundException; |
||
7 | use Mautic\CoreBundle\Helper\CoreParametersHelper; |
||
8 | |||
9 | class MaxMindDoNotSellList implements DoNotSellListInterface |
||
10 | { |
||
11 | private $position = 0; |
||
12 | |||
13 | private $list = []; |
||
14 | |||
15 | private $listPath; |
||
16 | |||
17 | public function __construct(CoreParametersHelper $coreParametersHelper) |
||
18 | { |
||
19 | $this->listPath = $coreParametersHelper->get('maxmind_do_not_sell_list_path', ''); |
||
20 | } |
||
21 | |||
22 | public function loadList(): bool |
||
23 | { |
||
24 | $listPath = $this->getListPath(); |
||
25 | |||
26 | if (false == $listPath) { |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
27 | throw new BadConfigurationException('Please configure the path to the MaxMind Do Not Sell List.'); |
||
28 | } |
||
29 | |||
30 | if (!file_exists($listPath)) { |
||
31 | throw new FileNotFoundException('Please make sure the MaxMind Do Not Sell List file has been downloaded.'); |
||
32 | } |
||
33 | |||
34 | $json = file_get_contents($listPath); |
||
35 | |||
36 | if ($data = json_decode($json, true)) { |
||
37 | $this->list = $data['exclusions']; |
||
38 | |||
39 | return true; |
||
40 | } |
||
41 | |||
42 | return false; |
||
43 | } |
||
44 | |||
45 | public function getListPath(): string |
||
46 | { |
||
47 | return $this->listPath; |
||
48 | } |
||
49 | |||
50 | public function setListPath(string $path): void |
||
51 | { |
||
52 | $this->listPath = $path; |
||
53 | } |
||
54 | |||
55 | public function getList(): array |
||
56 | { |
||
57 | return $this->list; |
||
58 | } |
||
59 | |||
60 | public function rewind() |
||
61 | { |
||
62 | $this->position = 0; |
||
63 | } |
||
64 | |||
65 | public function current() |
||
66 | { |
||
67 | return $this->list[$this->position]; |
||
68 | } |
||
69 | |||
70 | public function key() |
||
71 | { |
||
72 | return $this->position; |
||
73 | } |
||
74 | |||
75 | public function next() |
||
76 | { |
||
77 | ++$this->position; |
||
78 | } |
||
79 | |||
80 | public function valid() |
||
81 | { |
||
82 | return isset($this->list[$this->position]); |
||
83 | } |
||
84 | } |
||
85 |