Completed
Push — dev-master ( e8caf5...61b54b )
by Derek Stephen
01:29
created

Controller::initTranslator()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6.0087

Importance

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