Completed
Push — master ( 86865c...653b85 )
by Kevin
26s queued 24s
created

CustomErrorPages   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 21.74%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 65
ccs 5
cts 23
cp 0.2174
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A check() 0 19 4
A getLabel() 0 4 1
A getCustomTemplateDirectory() 0 12 3
1
<?php
2
3
namespace Liip\MonitorBundle\Check;
4
5
use Laminas\Diagnostics\Check\CheckInterface;
6
use Laminas\Diagnostics\Result\Failure;
7
use Laminas\Diagnostics\Result\Success;
8
9
/**
10
 * Checks if error pages have been customized.
11
 *
12
 * @author Cédric Girard <[email protected]>
13
 */
14
class CustomErrorPages implements CheckInterface
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $errorCodes;
20
21
    /**
22
     * @var string
23
     */
24
    protected $path;
25
26
    /**
27
     * @var string
28
     */
29
    protected $projectDir;
30
31 1
    public function __construct(array $errorCodes, $path, $projectDir)
32
    {
33 1
        $this->errorCodes = $errorCodes;
34 1
        $this->path = $path;
35 1
        $this->projectDir = $projectDir;
36 1
    }
37
38
    public function check()
39
    {
40
        $dir = $this->getCustomTemplateDirectory();
41
        $missingTemplates = [];
42
43
        foreach ($this->errorCodes as $errorCode) {
44
            $template = sprintf('%s/error%d.html.twig', $dir, $errorCode);
45
46
            if (!file_exists($template)) {
47
                $missingTemplates[] = $errorCode;
48
            }
49
        }
50
51
        if (count($missingTemplates) > 0) {
52
            return new Failure(sprintf('No custom error page found for the following codes: %s', implode(', ', $missingTemplates)));
53
        }
54
55
        return new Success();
56
    }
57
58
    public function getLabel()
59
    {
60
        return 'Custom error pages';
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    private function getCustomTemplateDirectory()
67
    {
68
        if ($this->projectDir !== $this->path) {
69
            return $this->path; // using custom directory
70
        }
71
72
        if (file_exists($dir = $this->projectDir.'/templates/bundles/TwigBundle/Exception')) {
73
            return $dir; // using standard 4.0+ directory
74
        }
75
76
        return $this->projectDir.'/app/Resources/TwigBundle/views/Exception'; // assume using 3.4 dir structure
77
    }
78
}
79