Completed
Pull Request — master (#2)
by
unknown
15:16
created

Varnish::shutdown()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.2
ccs 11
cts 11
cp 1
cc 4
eloc 9
nc 2
nop 0
crap 4
1
<?php
2
namespace Aoe\Varnish\System;
3
4
use Aoe\Varnish\Domain\Model\TagInterface;
5
use Aoe\Varnish\TYPO3\Configuration\ExtensionConfiguration;
6
use TYPO3\CMS\Core\Log\LogManager;
7
use TYPO3\CMS\Core\SingletonInterface;
8
9
class Varnish implements SingletonInterface
10
{
11
    /**
12
     * @var Http
13
     */
14
    private $http;
15
16
    /**
17
     * @var ExtensionConfiguration
18
     */
19
    private $extensionConfiguration;
20
21
    /**
22
     * @var LogManager
23
     */
24
    private $logManager;
25
26
    /**
27
     * @param Http $http
28
     * @param ExtensionConfiguration $extensionConfiguration
29
     * @param LogManager $logManager
30
     */
31 4
    public function __construct(Http $http, ExtensionConfiguration $extensionConfiguration, LogManager $logManager)
32
    {
33 4
        $this->http = $http;
34 4
        $this->extensionConfiguration = $extensionConfiguration;
35 4
        $this->logManager = $logManager;
36
37 4
        register_shutdown_function([$this, 'shutdown']);
38 4
    }
39
40
    /**
41
     * @return array
42
     */
43 1
    public function shutdown()
44
    {
45 1
        $phrases = $this->http->wait();
46 1
        if (is_array($phrases)) {
47 1
            foreach ($phrases as $phrase) {
48 1
                if ($phrase['success']) {
49 1
                    $this->logManager->getLogger(__CLASS__)->info($phrase['reason']);
50 1
                } else {
51 1
                    $this->logManager->getLogger(__CLASS__)->alert($phrase['reason']);
52
                }
53 1
            }
54 1
        }
55 1
        return $phrases;
56
    }
57
58
    /**
59
     * @param TagInterface $tag
60
     * @return Varnish
61
     */
62 2
    public function banByTag(TagInterface $tag)
63
    {
64 2
        if (false === $tag->isValid()) {
65 1
            throw new \RuntimeException('Tag is not valid', 1435159558);
66
        }
67 1
        $this->request('BAN', ['X-Ban-Tags' => $tag->getIdentifier()], $this->extensionConfiguration->getBanTimeout());
68 1
        return $this;
69
    }
70
71
    /**
72
     * @return Varnish
73
     */
74 1
    public function banAll()
75
    {
76 1
        $this->request('BAN', ['X-Ban-All' => '1'], $this->extensionConfiguration->getBanTimeout());
77 1
        return $this;
78
    }
79
80
    /**
81
     * @param string $method
82
     * @param array $headers
83
     * @param integer $timeout
84
     */
85 2
    private function request($method, $headers = [], $timeout = null)
86
    {
87 2
        if ($timeout === null) {
88
            $timeout = $this->extensionConfiguration->getDefaultTimeout();
89
        }
90
91 2
        foreach ($this->extensionConfiguration->getHosts() as $host) {
92 2
            $this->http->request($method, $host, $headers, $timeout);
93 2
        }
94 2
    }
95
}
96