Completed
Push — github-actions ( 673aea...a81297 )
by Samuel
01:18
created

AbstractAdapter::setTempDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of CacheTool.
5
 *
6
 * (c) Samuel Gordalina <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CacheTool\Adapter;
13
14
use CacheTool\Code;
15
use Psr\Log\LoggerInterface;
16
17
abstract class AbstractAdapter
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $tempDir;
23
24
    /**
25
     * @var LoggerInterface
26
     */
27
    protected $logger;
28
29
    /**
30
     * @param  Code   $code
31
     * @return string
32
     */
33
    abstract protected function doRun(Code $code);
34
35
    /**
36
     * @param  Code   $code
37
     * @throws \RuntimeException
38
     * @return mixed
39
     */
40 19
    public function run(Code $code)
41
    {
42 19
        $this->logger->debug(sprintf('Executing code: %s', $code->getCode()));
43 19
        $data = $this->doRun($code);
44
45 17
        $result = @unserialize($data);
46
47 17
        if (!is_array($result)) {
48
            $this->logger->debug(sprintf('Return serialized: %s', $data));
49
            throw new \RuntimeException('Could not unserialize data from adapter.');
50
        }
51
52 17
        $this->logger->debug(sprintf('Return errors: %s', json_encode($result['errors'])));
53 17
        $this->logger->debug(sprintf('Return result: %s', json_encode($result['result'])));
54
55 17
        if (empty($result['errors'])) {
56 16
            return $result['result'];
57
        }
58
59
        $errors = array_reduce($result['errors'], function ($carry, $error) {
60 1
            return $carry .= "{$error['str']} (error code: {$error['no']})\n";
61 1
        });
62
63 1
        throw new \RuntimeException($errors);
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function getTempDir()
70
    {
71
        return $this->tempDir;
72
    }
73
74
    /**
75
     * @param string $tempDir
76
     */
77 25
    public function setTempDir($tempDir)
78
    {
79 25
        $this->tempDir = $tempDir;
80
81 25
        return $this;
82
    }
83
84
    /**
85
     * @param  LoggerInterface $logger
86
     * @return AbstractAdapter
87
     */
88 25
    public function setLogger(LoggerInterface $logger)
89
    {
90 25
        $this->logger = $logger;
91
92 25
        return $this;
93
    }
94
95
    /**
96
     * @return string
97
     */
98 18
    protected function createTemporaryFile()
99
    {
100 18
        $file = sprintf("%s/cachetool-%s.php", $this->tempDir, uniqid());
101
102 18
        touch($file);
103 18
        chmod($file, 0666);
104
105 18
        return $file;
106
    }
107
}
108