AbstractBinary::isSupported()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
namespace Peridot\WebDriverManager\Binary;
3
4
/**
5
 * AbstractBinary is a base class for all binaries and drivers.
6
 *
7
 * @package Peridot\WebDriverManager\Binary
8
 */
9
abstract class AbstractBinary implements BinaryInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $contents;
15
16
    /**
17
     * @var BinaryResolverInterface
18
     */
19
    protected $resolver;
20
21
    /**
22
     * @param BinaryResolverInterface $resolver
23
     */
24
    public function __construct(BinaryResolverInterface $resolver)
25
    {
26
        $this->resolver = $resolver;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     *
32
     * @return bool
33
     */
34
    public function fetch()
35
    {
36
        $this->contents = $this->resolver->request($this->getUrl());
37
        return $this->contents !== false;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     *
43
     * @param string $directory
44
     * @return bool
45
     */
46
    public function save($directory)
47
    {
48
        if ($this->exists($directory)) {
49
            return true;
50
        }
51
52
        if (! $this->contents) {
53
            return false;
54
        }
55
56
        $output = $this->getDestination($directory);
57
        $this->removeOldVersions($directory);
58
        return file_put_contents($output, $this->contents) !== false;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     *
64
     * @param string $directory
65
     * @return bool
66
     */
67
    public function fetchAndSave($directory)
68
    {
69
        if ($this->exists($directory)) {
70
            return true;
71
        }
72
73
        return $this->fetch() && $this->save($directory);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     *
79
     * @return string
80
     */
81
    public function getContents()
82
    {
83
        return $this->contents;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     *
89
     * @param $directory
90
     * @return bool
91
     */
92
    public function exists($directory)
93
    {
94
        return file_exists($this->getDestination($directory));
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     *
100
     * @param $directory
101
     * @return bool
102
     */
103
    public function isOutOfDate($directory)
104
    {
105
        if ($this->exists($directory)) {
106
            return false;
107
        }
108
109
        $pattern = $this->getOldFilePattern($directory);
110
        $matches = glob($pattern);
111
112
        return count($matches) > 0;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     *
118
     * @return bool
119
     */
120
    public function isSupported()
121
    {
122
        return true;
123
    }
124
125
    /**
126
     * Get the destination file for the binary.
127
     *
128
     * @param $directory
129
     * @return string
130
     */
131
    protected function getDestination($directory)
132
    {
133
        return "$directory/{$this->getFileName()}";
134
    }
135
136
    /**
137
     * Remove old versions of the binary.
138
     *
139
     * @param $directory
140
     * @return void
141
     */
142
    protected function removeOldVersions($directory)
143
    {
144
145
    }
146
147
    /**
148
     * Return a pattern to identify old versions of a binary.
149
     *
150
     * @param string $directory
151
     * @return string
152
     */
153
    abstract protected function getOldFilePattern($directory);
154
}
155