ErrorCodes::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Phrest\API\Action;
4
5
class ErrorCodes implements \Interop\Http\ServerMiddleware\MiddlewareInterface
6
{
7
    /**
8
     * @var \Phrest\API\ErrorCodes
9
     */
10
    private $errorCodes;
11
12
    /**
13
     * @var \Zend\Cache\Storage\StorageInterface
14
     */
15
    private $cache;
16
17
    private const CACHE_ERROR_CODES = 'Phrest_API_Action_ErrorCodes';
18
19
    public function __construct(\Zend\Cache\Storage\StorageInterface $cache, \Phrest\API\ErrorCodes $errorCodes)
20
    {
21
        $this->errorCodes = $errorCodes;
22
        $this->cache = $cache;
23
    }
24
25
    public function process(\Psr\Http\Message\ServerRequestInterface $request, \Interop\Http\ServerMiddleware\DelegateInterface $delegate)
26
    {
27
        if(!$this->cache->hasItem(self::CACHE_ERROR_CODES)) {
28
            $errorCodes = $this->errorCodes->getErrorCodes();
29
            $this->cache->setItem(self::CACHE_ERROR_CODES, serialize($errorCodes));
30
        } else {
31
            $errorCodes = unserialize($this->cache->getItem(self::CACHE_ERROR_CODES));
32
        }
33
34
        return new \Zend\Diactoros\Response\JsonResponse($errorCodes);
35
    }
36
}