Completed
Pull Request — master (#2)
by Felix
03:33
created

Varnish::request()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 6
cts 8
cp 0.75
rs 9.4285
cc 3
eloc 5
nc 4
nop 3
crap 3.1406
1
<?php
2
namespace Aoe\Varnish\System;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2017 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use Aoe\Varnish\Domain\Model\TagInterface;
29
use Aoe\Varnish\TYPO3\Configuration\ExtensionConfiguration;
30
use TYPO3\CMS\Core\Log\LogManager;
31
use TYPO3\CMS\Core\SingletonInterface;
32
33
class Varnish implements SingletonInterface
34
{
35
    /**
36
     * @var Http
37
     */
38
    private $http;
39
40
    /**
41
     * @var ExtensionConfiguration
42
     */
43
    private $extensionConfiguration;
44
45
    /**
46
     * @var LogManager
47
     */
48
    private $logManager;
49
50
    /**
51
     * @param Http $http
52
     * @param ExtensionConfiguration $extensionConfiguration
53
     * @param LogManager $logManager
54
     */
55 4
    public function __construct(Http $http, ExtensionConfiguration $extensionConfiguration, LogManager $logManager)
56
    {
57 4
        $this->http = $http;
58 4
        $this->extensionConfiguration = $extensionConfiguration;
59 4
        $this->logManager = $logManager;
60
61 4
        register_shutdown_function([$this, 'shutdown']);
62 4
    }
63
64
    /**
65
     * @return array
66
     */
67 1
    public function shutdown()
68
    {
69 1
        $phrases = $this->http->wait();
70 1
        if (is_array($phrases)) {
71 1
            foreach ($phrases as $phrase) {
72 1
                if ($phrase['success']) {
73 1
                    $this->logManager->getLogger(__CLASS__)->info($phrase['reason']);
74 1
                } else {
75 1
                    $this->logManager->getLogger(__CLASS__)->alert($phrase['reason']);
76
                }
77 1
            }
78 1
        }
79 1
        return $phrases;
80
    }
81
82
    /**
83
     * @param TagInterface $tag
84
     * @return Varnish
85
     */
86 2
    public function banByTag(TagInterface $tag)
87
    {
88 2
        if (false === $tag->isValid()) {
89 1
            throw new \RuntimeException('Tag is not valid', 1435159558);
90
        }
91 1
        $this->request('BAN', ['X-Ban-Tags' => $tag->getIdentifier()], $this->extensionConfiguration->getBanTimeout());
92 1
        return $this;
93
    }
94
95
    /**
96
     * @return Varnish
97
     */
98 1
    public function banAll()
99
    {
100 1
        $this->request('BAN', ['X-Ban-All' => '1'], $this->extensionConfiguration->getBanTimeout());
101 1
        return $this;
102
    }
103
104
    /**
105
     * @param string $method
106
     * @param array $headers
107
     * @param integer $timeout
108
     */
109 2
    private function request($method, $headers = [], $timeout = null)
110
    {
111 2
        if ($timeout === null) {
112
            $timeout = $this->extensionConfiguration->getDefaultTimeout();
113
        }
114
115 2
        foreach ($this->extensionConfiguration->getHosts() as $host) {
116 2
            $this->http->request($method, $host, $headers, $timeout);
117 2
        }
118 2
    }
119
}
120