Completed
Push — master ( 31b5db...524ecf )
by Michael
03:26
created

SqlWiring::wireCreator()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 37
ccs 0
cts 29
cp 0
rs 8.5806
cc 4
eloc 23
nc 4
nop 1
crap 20
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * Contains class SqlWiring.
5
 *
6
 * PHP version 7.0+
7
 *
8
 * LICENSE:
9
 * This file is part of Yet Another Php Eve Api Library also know as Yapeal
10
 * which can be used to access the Eve Online API data and place it into a
11
 * database.
12
 * Copyright (C) 2016 Michael Cummings
13
 *
14
 * This program is free software: you can redistribute it and/or modify it
15
 * under the terms of the GNU Lesser General Public License as published by the
16
 * Free Software Foundation, either version 3 of the License, or (at your
17
 * option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful, but WITHOUT
20
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
22
 * for more details.
23
 *
24
 * You should have received a copy of the GNU Lesser General Public License
25
 * along with this program. If not, see
26
 * <http://spdx.org/licenses/LGPL-3.0.html>.
27
 *
28
 * You should be able to find a copy of this license in the COPYING-LESSER.md
29
 * file. A copy of the GNU GPL should also be available in the COPYING.md file.
30
 *
31
 * @copyright 2016 Michael Cummings
32
 * @license   LGPL-3.0+
33
 * @author    Michael Cummings <[email protected]>
34
 */
35
namespace Yapeal\Configuration;
36
37
use Yapeal\Container\ContainerInterface;
38
use Yapeal\Exception\YapealDatabaseException;
39
40
/**
41
 * Class SqlWiring.
42
 */
43
class SqlWiring implements WiringInterface
44
{
45
    /**
46
     * @param ContainerInterface $dic
47
     *
48
     * @throws \InvalidArgumentException
49
     * @throws \LogicException
50
     * @throws \Yapeal\Exception\YapealDatabaseException
51
     */
52
    public function wire(ContainerInterface $dic)
53
    {
54
        if (empty($dic['Yapeal.Sql.CommonQueries'])) {
55
            $dic['Yapeal.Sql.CommonQueries'] = function ($dic) {
56
                return new $dic['Yapeal.Sql.Handlers.queries']($dic['Yapeal.Sql.database'],
57
                    $dic['Yapeal.Sql.tablePrefix']);
58
            };
59
        }
60
        if (empty($dic['Yapeal.Sql.Connection'])) {
61
            if ('mysql' !== $dic['Yapeal.Sql.platform']) {
62
                $mess = 'Unknown platform, was given ' . $dic['Yapeal.Sql.platform'];
63
                throw new YapealDatabaseException($mess);
64
            }
65
            $dic['Yapeal.Sql.Connection'] = function ($dic) {
66
                $dsn = '%1$s:host=%2$s;charset=utf8mb4';
67
                $subs = [$dic['Yapeal.Sql.platform'], $dic['Yapeal.Sql.hostName']];
68
                if (!empty($dic['Yapeal.Sql.port'])) {
69
                    $dsn .= ';port=%3$s';
70
                    $subs[] = $dic['Yapeal.Sql.port'];
71
                }
72
                /**
73
                 * @var \PDO $database
74
                 */
75
                $database = new $dic['Yapeal.Sql.Handlers.connection'](vsprintf($dsn, $subs),
76
                    $dic['Yapeal.Sql.userName'],
77
                    $dic['Yapeal.Sql.password']);
78
                $database->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
79
                $database->exec('SET SESSION SQL_MODE=\'ANSI,TRADITIONAL\'');
80
                $database->exec('SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE');
81
                $database->exec('SET SESSION TIME_ZONE=\'+00:00\'');
82
                $database->exec('SET NAMES utf8mb4 COLLATE utf8mb4_unicode_520_ci');
83
                $database->exec('SET COLLATION_CONNECTION=utf8mb4_unicode_520_ci');
84
                $database->exec('SET DEFAULT_STORAGE_ENGINE=' . $dic['Yapeal.Sql.engine']);
85
                return $database;
86
            };
87
        }
88
        $this->wireCreator($dic);
89
    }
90
    /**
91
     * @param \Yapeal\Container\ContainerInterface $dic
92
     *
93
     * @throws \LogicException
94
     */
95
    private function wireCreator(ContainerInterface $dic)
96
    {
97
        if (empty($dic['Yapeal.Sql.Creator'])) {
98
            $dic['Yapeal.Sql.Creator'] = function () use ($dic) {
99
                $loader = new \Twig_Loader_Filesystem($dic['Yapeal.Sql.dir']);
100
                $twig = new \Twig_Environment($loader,
101
                    ['debug' => true, 'strict_variables' => true, 'autoescape' => false]);
102
                $filter = new \Twig_SimpleFilter('ucFirst', function ($value) {
103
                    return ucfirst($value);
104
                });
105
                $twig->addFilter($filter);
106
                $filter = new \Twig_SimpleFilter('lcFirst', function ($value) {
107
                    return lcfirst($value);
108
                });
109
                $twig->addFilter($filter);
110
                /**
111
                 * @var \Yapeal\Sql\Creator $create
112
                 */
113
                $create = new $dic['Yapeal.Sql.Handlers.create']($twig,
114
                    $dic['Yapeal.Sql.dir'],
115
                    $dic['Yapeal.Sql.platform']);
116
                if (!empty($dic['Yapeal.Create.overwrite'])) {
117
                    $create->setOverwrite($dic['Yapeal.Create.overwrite']);
118
                }
119
                return $create;
120
            };
121
        }
122
        if (empty($dic['Yapeal.Event.Mediator'])) {
123
            $mess = 'Tried to call Mediator before it has been added';
124
            throw new \LogicException($mess);
125
        }
126
        /**
127
         * @var \Yapeal\Event\MediatorInterface $mediator
128
         */
129
        $mediator = $dic['Yapeal.Event.Mediator'];
130
        $mediator->addServiceListener('Yapeal.EveApi.create', ['Yapeal.Sql.Creator', 'createSql'], 'last');
131
    }
132
}
133