Completed
Push — master ( cc7fe7...f1fa2a )
by Michael
02:58
created

CommonToolsTrait::setDic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * Contains CommandToolsTrait trait.
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) 2014-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 2014-2016 Michael Cummings
32
 * @license   http://www.gnu.org/copyleft/lesser.html GNU LGPL
33
 * @author    Michael Cummings <[email protected]>
34
 */
35
namespace Yapeal;
36
37
use Yapeal\Exception\YapealDatabaseException;
38
use Yapeal\Sql\CommonSqlQueries;
39
40
/**
41
 * Trait CommandToolsTrait
42
 */
43
trait CommonToolsTrait
44
{
45
    use DicAwareTrait;
46
    /**
47
     * @param CommonSqlQueries $value
48
     *
49
     * @return self Fluent interface.
50
     */
51 1
    public function setCsq(CommonSqlQueries $value)
52
    {
53 1
        $this->csq = $value;
54 1
        return $this;
55
    }
56
    /**
57
     * @param \PDO $value
58
     *
59
     * @return self Fluent interface.
60
     */
61 1
    public function setPdo(\PDO $value)
62
    {
63 1
        $this->pdo = $value;
64 1
        return $this;
65
    }
66
    /**
67
     * @return CommonSqlQueries
68
     * @throws \LogicException
69
     */
70
    protected function getCsq(): CommonSqlQueries
71
    {
72
        if (null === $this->csq) {
73
            $this->csq = $this->getDic()['Yapeal.Sql.CommonQueries'];
74
        }
75
        if (!$this->csq instanceof CommonSqlQueries) {
76
            $mess = 'Tried to use csq before it was set';
77
            throw new \LogicException($mess, 1);
78
        }
79
        return $this->csq;
80
    }
81
    /**
82
     * @return \PDO
83
     * @throws \LogicException
84
     * @throws \Yapeal\Exception\YapealDatabaseException
85
     */
86
    protected function getPdo(): \PDO
87
    {
88
        if (null === $this->pdo) {
89
            try {
90
                $this->pdo = $this->getDic()['Yapeal.Sql.Connection'];
91
            } catch (\PDOException $exc) {
92
                $mess = sprintf('Could NOT connect to database. Database error was (%1$s) %2$s',
93
                    $exc->getCode(),
94
                    $exc->getMessage());
95
                throw new YapealDatabaseException($mess, 1, $exc);
96
            }
97
        }
98
        return $this->pdo;
99
    }
100
    /**
101
     * @var CommonSqlQueries $csq
102
     */
103
    protected $csq;
104
    /**
105
     * @var \PDO $pdo
106
     */
107
    protected $pdo;
108
}
109