Completed
Push — dev-master ( 61317d...8c136d )
by Derek Stephen
29:12 queued 26:15
created

Controller::errorAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Bone\Mvc;
4
5
use Bone\Db\Adapter\MySQL;
6
use Bone\Mvc\View\Extension\Plates\Translate;
7
use Bone\Server\Environment;
8
use Bone\Service\LoggerFactory;
9
use Bone\Service\MailService;
10
use Bone\Service\TranslatorFactory;
11
use InvalidArgumentException;
12
use League\Plates\Engine;
13
use LogicException;
14
use Monolog\Logger;
15
use Psr\Http\Message\ServerRequestInterface;
16
use Zend\Diactoros\Response\TextResponse;
17
use Zend\Mail\Transport\Smtp;
18
use Zend\Mail\Transport\SmtpOptions;
19
20
class Controller extends AbstractController
21
{
22
    /** @var MailService $mailService */
23
    private $mailService;
24
25
    /** @var Logger[] $log */
26
    protected $log;
27
28
    protected $translator;
29
30
    /**
31
     * Controller constructor.
32
     * @param ServerRequestInterface $request
33
     * @throws \Exception
34
     */
35 33
    public function __construct(ServerRequestInterface $request)
36 33
    {
37 33
        parent::__construct($request);
38 33
        $this->initTranslator();
39 33
        $this->initLogs();
40 33
    }
41
42
    /**
43
     * @return MailService
44
     */
45 1
    public function getMailService()
46 1
    {
47 1
        if (!$this->mailService instanceof MailService) {
48 1
            $this->initMailService();
49
        }
50
51 1
        return $this->mailService;
52
    }
53
54 1
    private function initMailService()
55 1
    {
56 1
        $this->mailService = new MailService();
57 1
        $options = Registry::ahoy()->get('mail');
58 1
        if (isset($options['name']) && isset($options['host']) && isset($options['port']) ) {
59 1
            $transport = new Smtp();
60 1
            $options   = new SmtpOptions($options);
61 1
            $transport->setOptions($options);
62 1
            $this->mailService->setTransport($transport);
63
        }
64 1
    }
65
66
    /**
67
     *  runs before th' controller action
68
     */
69 24
    public function init()
70 24
    {
71
        // extend this t' initialise th' controller
72 24
    }
73
74
    /**
75
     *  runs after yer work is done
76
     */
77 24
    public function postDispatch()
78 24
    {
79
        // extend this t' run code after yer controller is finished
80 24
    }
81
82
    /**
83
     * @return array
84
     */
85 1
    public function indexAction()
86 1
    {
87 1
        return ['message' => 'Override this method'];
88
    }
89
90 2
    public function errorAction()
91 2
    {
92 2
        $this->disableView();
93 2
        $this->disableLayout();
94 2
        $this->setBody('500 Page Error.');
95 2
        return new TextResponse($this->getBody(), 500);
96
    }
97
98 1
    public function notFoundAction()
99 1
    {
100 1
        $this->disableView();
101 1
        $this->disableLayout();
102 1
        $this->setBody('404 Page Not Found.');
103 1
    }
104
105
    /**
106
     * @param string $channel
107
     * @return Logger
108
     */
109 2
    public function getLog($channel = 'default'): Logger
110 2
    {
111 2
        if (!$this->log) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->log of type Monolog\Logger[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
112 1
            throw new LogicException('No log config found');
113
        }
114
115 1
        if (!isset($this->log[$channel])) {
116
            throw new InvalidArgumentException('No log channel with that name found');
117
        }
118
119 1
        return $this->log[$channel];
120
    }
121
122
    /**
123
     * @throws \Exception
124
     */
125 33
    private function initLogs()
126 33
    {
127 33
        $config = Registry::ahoy()->get('log');
128 33
        if (is_array($config)) {
129 2
            $factory = new LoggerFactory();
130 2
            $logs = $factory->createLoggers($config);
131 1
            $this->log = $logs;
132
        }
133 33
    }
134
135
    /**
136
     * @return \Zend\I18n\Translator\Translator
137
     */
138 3
    public function getTranslator()
139 3
    {
140 3
        if (!$this->translator) {
141
            throw new LogicException('No i18n config found');
142
        }
143
144 3
        return $this->translator;
145
    }
146
147 33
    private function initTranslator()
148 33
    {
149 33
        $config = Registry::ahoy()->get('i18n');
150 33
        if (is_array($config) && !$this->translator) {
151
152 14
            $factory = new TranslatorFactory();
153 14
            $translator = $factory->createTranslator($config);
154
155 14
            $engine = $this->getViewEngine();
156 14
            if ($engine instanceof Engine) {
157 14
                $engine->loadExtension(new Translate($translator));
158
            }
159
160 14
            $this->translator = $translator;
161
        }
162 33
    }
163
}
164