InitController::onDispatch()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace T4web\Mail\Controller;
4
5
use Zend\Mvc\Controller\AbstractActionController;
6
use Zend\Console\Request as ConsoleRequest;
7
use Zend\Mvc\MvcEvent;
8
use Zend\Db\Adapter\Adapter as DbAdapter;
9
10
class InitController extends AbstractActionController
11
{
12
    /**
13
     * @var DbAdapter
14
     */
15
    private $dbAdapter;
16
17
    public function __construct(DbAdapter $dbAdapter)
18
    {
19
        $this->dbAdapter = $dbAdapter;
20
    }
21
22
    public function onDispatch(MvcEvent $e)
23
    {
24
        if (!$e->getRequest() instanceof ConsoleRequest) {
25
            throw new \RuntimeException('You can only use this action from a console!');
26
        }
27
28
        $query = "CREATE TABLE IF NOT EXISTS `mail_log` (
29
              `id` INT(11) NOT NULL AUTO_INCREMENT,
30
              `mail_from` VARCHAR(100) DEFAULT NULL,
31
              `mail_to` VARCHAR(100) DEFAULT NULL,
32
              `subject` VARCHAR(250) DEFAULT NULL,
33
              `layout_id` INT DEFAULT 0,
34
              `template_id` INT DEFAULT 0,
35
              `body` TEXT DEFAULT NULL,
36
              `calculated_vars` TEXT DEFAULT NULL,
37
              `created_dt` datetime NOT NULL,
38
              PRIMARY KEY (`id`)
39
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;";
40
41
        $this->createTable($query);
42
    }
43
44
    private function createTable($query)
45
    {
46
        try {
47
            $this->dbAdapter->query($query, DbAdapter::QUERY_MODE_EXECUTE);
48
        } catch (\PDOException $e) {
49
            if (strpos($e->getMessage(), 'table or view already exists') === false) {
50
                echo $e->getMessage() . PHP_EOL;
51
                exit(1);
0 ignored issues
show
Coding Style Compatibility introduced by
The method createTable() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
52
            }
53
        } catch (\Exception $e) {
54
            echo  $e->getMessage() . PHP_EOL;
55
            exit(1);
0 ignored issues
show
Coding Style Compatibility introduced by
The method createTable() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
56
        }
57
    }
58
}
59