Completed
Push — dev-master ( 8ed987...1e9198 )
by Derek Stephen
04:46
created

Controller   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 9

Test Coverage

Coverage 97.26%

Importance

Changes 0
Metric Value
wmc 23
lcom 4
cbo 9
dl 0
loc 150
ccs 71
cts 73
cp 0.9726
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getMailService() 0 8 2
A initMailService() 0 11 4
A init() 0 4 1
A postDispatch() 0 4 1
A indexAction() 0 4 1
A errorAction() 0 7 1
A notFoundAction() 0 6 1
A getLog() 0 12 3
A initLogs() 0 9 2
A getTranslator() 0 8 2
A initTranslator() 0 16 4
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\Mvc\View\ViewEngine;
8
use Bone\Mvc\View\PlatesEngine;
9
use Bone\Server\Environment;
10
use Bone\Service\LoggerFactory;
11
use Bone\Service\MailService;
12
use Bone\Service\TranslatorFactory;
13
use InvalidArgumentException;
14
use League\Plates\Engine;
15
use LogicException;
16
use Monolog\Logger;
17
use PDO;
18
use Psr\Http\Message\ServerRequestInterface;
19
use stdClass;
20
use Zend\Diactoros\Response\TextResponse;
21
use Zend\Mail\Transport\Smtp;
22
use Zend\Mail\Transport\SmtpOptions;
23
24
class Controller extends AbstractController
25
{
26
    /** @var MailService $mailService */
27
    private $mailService;
28
29
    /** @var MySQL */
30
    protected $_db;
31
32
    /** @var Environment $serverEnvironment */
33
    protected $serverEnvironment;
34
35
    /** @var Logger[] $log */
36
    protected $log;
37
38
    protected $translator;
39
40
    /**
41
     * Controller constructor.
42
     * @param ServerRequestInterface $request
43
     * @throws \Exception
44
     */
45 33
    public function __construct(ServerRequestInterface $request)
46 33
    {
47 33
        parent::__construct($request);
48 33
        $this->initTranslator();
49 33
        $this->initLogs();
50 33
    }
51
52
    /**
53
     * @return MailService
54
     */
55 1
    public function getMailService()
56 1
    {
57 1
        if (!$this->mailService instanceof MailService) {
58 1
            $this->initMailService();
59
        }
60
61 1
        return $this->mailService;
62
    }
63
64 1
    private function initMailService()
65 1
    {
66 1
        $this->mailService = new MailService();
67 1
        $options = Registry::ahoy()->get('mail');
68 1
        if (isset($options['name']) && isset($options['host']) && isset($options['port']) ) {
69 1
            $transport = new Smtp();
70 1
            $options   = new SmtpOptions($options);
71 1
            $transport->setOptions($options);
72 1
            $this->mailService->setTransport($transport);
73
        }
74 1
    }
75
76
    /**
77
     *  runs before th' controller action
78
     */
79 24
    public function init()
80 24
    {
81
        // extend this t' initialise th' controller
82 24
    }
83
84
    /**
85
     *  runs after yer work is done
86
     */
87 24
    public function postDispatch()
88 24
    {
89
        // extend this t' run code after yer controller is finished
90 24
    }
91
92
    /**
93
     * @return array
94
     */
95 1
    public function indexAction()
96 1
    {
97 1
        return ['message' => 'Override this method'];
98
    }
99
100 2
    public function errorAction()
101 2
    {
102 2
        $this->disableView();
103 2
        $this->disableLayout();
104 2
        $this->setBody('500 Page Error.');
105 2
        return new TextResponse($this->getBody(), 500);
106
    }
107
108 1
    public function notFoundAction()
109 1
    {
110 1
        $this->disableView();
111 1
        $this->disableLayout();
112 1
        $this->setBody('404 Page Not Found.');
113 1
    }
114
115
    /**
116
     * @param string $channel
117
     * @return Logger
118
     */
119 2
    public function getLog($channel = 'default'): Logger
120 2
    {
121 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...
122 1
            throw new LogicException('No log config found');
123
        }
124
125 1
        if (!isset($this->log[$channel])) {
126
            throw new InvalidArgumentException('No log channel with that name found');
127
        }
128
129 1
        return $this->log[$channel];
130
    }
131
132
    /**
133
     * @throws \Exception
134
     */
135 33
    private function initLogs()
136 33
    {
137 33
        $config = Registry::ahoy()->get('log');
138 33
        if (is_array($config)) {
139 2
            $factory = new LoggerFactory();
140 2
            $logs = $factory->createLoggers($config);
141 1
            $this->log = $logs;
142
        }
143 33
    }
144
145
    /**
146
     * @return \Zend\I18n\Translator\Translator
147
     */
148 3
    public function getTranslator()
149 3
    {
150 3
        if (!$this->translator) {
151
            throw new LogicException('No i18n config found');
152
        }
153
154 3
        return $this->translator;
155
    }
156
157 33
    private function initTranslator()
158 33
    {
159 33
        $config = Registry::ahoy()->get('i18n');
160 33
        if (is_array($config) && !$this->translator) {
161
162 14
            $factory = new TranslatorFactory();
163 14
            $translator = $factory->createTranslator($config);
164
165 14
            $engine = $this->getViewEngine();
166 14
            if ($engine instanceof Engine) {
167 14
                $engine->loadExtension(new Translate($translator));
168
            }
169
170 14
            $this->translator = $translator;
171
        }
172 33
    }
173
}
174