Completed
Push — dev-master ( 86a9d6...915e4b )
by Derek Stephen
05:17
created

Controller::initMailService()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.9
c 0
b 0
f 0
cc 4
nc 2
nop 0
crap 4
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 34
    public function __construct(ServerRequestInterface $request)
34
    {
35 34
        parent::__construct($request);
36 34
        $this->initTranslator();
37 34
        $this->initLogs();
38 34
    }
39
40
    /**
41
     * @return MailService
42
     */
43 1
    public function getMailService()
44
    {
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
    {
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 25
    public function init()
68
    {
69
        // extend this t' initialise th' controller
70 25
    }
71
72
    /**
73
     *  runs after yer work is done
74
     */
75 25
    public function postDispatch()
76
    {
77
        // extend this t' run code after yer controller is finished
78 25
    }
79
80
    /**
81
     * @return array
82
     */
83 1
    public function indexAction()
84
    {
85 1
        return ['message' => 'Override this method'];
86
    }
87
88 3
    public function errorAction()
89
    {
90 3
        $this->disableView();
91 3
        $this->disableLayout();
92 3
        $this->setBody('500 Page Error.');
93 3
        return new TextResponse($this->getBody(), 500);
94
    }
95
96 2
    public function notFoundAction()
97
    {
98 2
        $this->disableView();
99 2
        $this->disableLayout();
100 2
        $this->setBody('404 Page Not Found.');
101 2
    }
102
103
    /**
104
     * @param string $channel
105
     * @return Logger
106
     */
107 2
    public function getLog($channel = 'default'): Logger
108
    {
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 34
    private function initLogs()
124
    {
125 34
        $config = Registry::ahoy()->get('log');
126 34
        if (is_array($config)) {
127 2
            $factory = new LoggerFactory();
128 2
            $logs = $factory->createLoggers($config);
129 1
            $this->log = $logs;
130
        }
131 34
    }
132
133
    /**
134
     * @return \Zend\I18n\Translator\Translator
135
     */
136 3
    public function getTranslator()
137
    {
138 3
        if (!$this->translator) {
139
            throw new LogicException('No i18n config found');
140
        }
141
142 3
        return $this->translator;
143
    }
144
145 34
    private function initTranslator()
146
    {
147 34
        $config = Registry::ahoy()->get('i18n');
148 34
        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 34
    }
168
}
169