Completed
Push — master ( 75748b...36048b )
by Michael
06:58
created

CommonToolsTrait::setCsq()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * Contains CommandToolsTrait trait.
4
 *
5
 * PHP version 5.5
6
 *
7
 * LICENSE:
8
 * This file is part of Yet Another Php Eve Api Library also know as Yapeal
9
 * which can be used to access the Eve Online API data and place it into a
10
 * database.
11
 * Copyright (C) 2014-2016 Michael Cummings
12
 *
13
 * This program is free software: you can redistribute it and/or modify it
14
 * under the terms of the GNU Lesser General Public License as published by the
15
 * Free Software Foundation, either version 3 of the License, or (at your
16
 * option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful, but WITHOUT
19
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
21
 * for more details.
22
 *
23
 * You should have received a copy of the GNU Lesser General Public License
24
 * along with this program. If not, see
25
 * <http://www.gnu.org/licenses/>.
26
 *
27
 * You should be able to find a copy of this license in the LICENSE.md file. A
28
 * copy of the GNU GPL should also be available in the GNU-GPL.md file.
29
 *
30
 * @copyright 2014-2016 Michael Cummings
31
 * @license   http://www.gnu.org/copyleft/lesser.html GNU LGPL
32
 * @author    Michael Cummings <[email protected]>
33
 */
34
namespace Yapeal;
35
36
use PDO;
37
use PDOException;
38
use Yapeal\Container\ContainerInterface;
39
use Yapeal\Exception\YapealDatabaseException;
40
use Yapeal\Sql\CommonSqlQueries;
41
42
/**
43
 * Trait CommandToolsTrait
44
 */
45
trait CommonToolsTrait
46
{
47
    /**
48
     * @param CommonSqlQueries $value
49
     *
50
     * @return self Fluent interface.
51
     */
52
    public function setCsq(CommonSqlQueries $value)
53
    {
54
        $this->csq = $value;
55
        return $this;
56
    }
57
    /**
58
     * @param ContainerInterface $value
59
     *
60
     * @return self Fluent interface.
61
     */
62
    public function setDic(ContainerInterface $value)
63
    {
64
        $this->dic = $value;
65
        return $this;
66
    }
67
    /**
68
     * @param PDO $value
69
     *
70
     * @return self Fluent interface.
71
     */
72
    public function setPdo(PDO $value)
73
    {
74
        $this->pdo = $value;
75
        return $this;
76
    }
77
    /**
78
     * @return CommonSqlQueries
79
     * @throws \LogicException
80
     */
81
    protected function getCsq()
82
    {
83
        if (null === $this->csq) {
84
            $this->csq = $this->getDic()['Yapeal.Sql.CommonQueries'];
85
        }
86
        if (!$this->csq instanceof CommonSqlQueries) {
87
            $mess = 'Tried to use csq before it was set';
88
            throw new \LogicException($mess, 1);
89
        }
90
        return $this->csq;
91
    }
92
    /**
93
     * @return ContainerInterface
94
     * @throws \LogicException
95
     */
96
    protected function getDic()
97
    {
98
        if (!$this->dic instanceof ContainerInterface) {
99
            $mess = 'Tried to use dic before it was set';
100
            throw new \LogicException($mess, 1);
101
        }
102
        return $this->dic;
103
    }
104
    /**
105
     * @return PDO
106
     * @throws \LogicException
107
     * @throws \Yapeal\Exception\YapealDatabaseException
108
     */
109
    protected function getPdo()
110
    {
111
        if (null === $this->pdo) {
112
            try {
113
                $this->pdo = $this->getDic()['Yapeal.Sql.Connection'];
114
            } catch (PDOException $exc) {
115
                $mess = sprintf(
116
                    'Could NOT connect to database. Database error was (%1$s) %2$s',
117
                    $exc->getCode(),
118
                    $exc->getMessage()
119
                );
120
                throw new YapealDatabaseException($mess, 1, $exc);
121
            }
122
        }
123
        return $this->pdo;
124
    }
125
    /**
126
     * @var CommonSqlQueries $csq
127
     */
128
    protected $csq;
129
    /**
130
     * @var ContainerInterface $dic
131
     */
132
    protected $dic;
133
    /**
134
     * @var PDO $pdo
135
     */
136
    protected $pdo;
137
}
138