Yapeal::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 4
crap 2
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * Contains Yapeal class.
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-2017 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-2017 Michael Cummings
32
 * @license   LGPL-3.0+
33
 * @author    Michael Cummings <[email protected]>
34
 */
35
namespace Yapeal;
36
37
use Yapeal\Event\EveApiEventEmitterTrait;
38
use Yapeal\Event\MediatorInterface;
39
use Yapeal\Log\Logger;
40
use Yapeal\Sql\CommonSqlQueries;
41
use Yapeal\Sql\CSQAwareTrait;
42
use Yapeal\Sql\PDOAwareTrait;
43
use Yapeal\Sql\ConnectionInterface;
44
use Yapeal\Xml\EveApiReadWriteInterface;
45
46
/**
47
 * Class Yapeal
48
 */
49
class Yapeal
50
{
51
    use CSQAwareTrait;
52
    use PDOAwareTrait;
53
    use EveApiEventEmitterTrait;
54
    /**
55
     * @param CommonSqlQueries         $csq
56
     * @param EveApiReadWriteInterface $data
57
     * @param ConnectionInterface      $pdo
58
     * @param MediatorInterface        $yem
59
     *
60
     * @throws \InvalidArgumentException
61
     */
62
    public function __construct(
63
        CommonSqlQueries $csq,
64
        EveApiReadWriteInterface $data,
65
        ConnectionInterface $pdo,
66
        MediatorInterface $yem
67
    ) {
68
        $this->setCsq($csq);
69
        $this->data = $data;
70
        $this->setPdo($pdo);
71
        $this->setYem($yem);
72
    }
73
    /**
74
     * Starts Eve API processing
75
     *
76
     * @return int Returns 0 if everything was fine else something >= 1 for any
77
     * errors.
78
     * @throws \DomainException
79
     * @throws \InvalidArgumentException
80
     * @throws \LogicException
81
     * @throws \UnexpectedValueException
82
     */
83
    public function autoMagic()
84
    {
85
        $mess = 'Let the magic begin!';
86
        $this->getYem()
87
            ->triggerLogEvent('Yapeal.Log.log', Logger::INFO, $mess);
88
        $sql = $this->getCsq()
89
            ->getActiveApis();
90
        $this->getYem()
91
            ->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, 'sql - ' . $sql);
92
        try {
93
            $records = $this->getPdo()
94
                ->query($sql)
95
                ->fetchAll(\PDO::FETCH_ASSOC);
96
        } catch (\PDOException $exc) {
97
            $mess = 'Could not access yapealEveApi table';
98
            $this->getYem()
99
                ->triggerLogEvent('Yapeal.Log.log', Logger::CRITICAL, $mess, ['exception' => $exc]);
100
            return 1;
101
        }
102
        // Always check APIKeyInfo first.
103
        array_unshift($records, ['apiName' => 'APIKeyInfo', 'interval' => 300, 'sectionName' => 'account']);
104
        foreach ($records as $record) {
105
            $data = clone $this->data;
106
            $data->setEveApiName($record['apiName'])
107
                ->setEveApiSectionName($record['sectionName'])
108
                ->setCacheInterval((int)$record['interval']);
109
            $this->emitEvents($data, 'start');
110
        }
111
        return 0;
112
    }
113
    /**
114
     * @var EveApiReadWriteInterface $data
115
     */
116
    private $data;
117
}
118