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

EveApiCreatorTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 98
ccs 0
cts 41
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isOverwrite() 0 4 1
A setDir() 0 5 1
A setOverwrite() 0 5 1
A setTwig() 0 5 1
A getDir() 0 4 1
A getTemplateName() 0 15 3
A getTwig() 0 4 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Contains EveApiCreatorTrait 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) 2015-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 2015-2016 Michael Cummings
32
 * @license   http://www.gnu.org/copyleft/lesser.html GNU LGPL
33
 * @author    Michael Cummings <[email protected]>
34
 */
35
namespace Yapeal\Console\Command;
36
37
use Twig_Environment;
38
use Yapeal\Event\EveApiEventEmitterTrait;
39
use Yapeal\FileSystem\CommonFileHandlingTrait;
40
41
/**
42
 * Trait EveApiCreatorTrait
43
 */
44
trait EveApiCreatorTrait
45
{
46
    use CommonFileHandlingTrait, EveApiEventEmitterTrait;
47
    /**
48
     * Getter for $overwrite.
49
     *
50
     * @return boolean
51
     */
52
    public function isOverwrite(): bool
53
    {
54
        return $this->overwrite;
55
    }
56
    /**
57
     * @param string $value
58
     *
59
     * @return self Fluent interface.
60
     */
61
    public function setDir($value)
62
    {
63
        $this->dir = (string)$value;
64
        return $this;
65
    }
66
    /**
67
     * Fluent interface setter for $overwrite.
68
     *
69
     * @param bool $value
70
     *
71
     * @return self Fluent interface.
72
     */
73
    public function setOverwrite(bool $value = true)
74
    {
75
        $this->overwrite = (bool)$value;
76
        return $this;
77
    }
78
    /**
79
     * @param Twig_Environment $twig
80
     *
81
     * @return self Fluent interface.
82
     */
83
    public function setTwig(Twig_Environment $twig)
84
    {
85
        $this->twig = $twig;
86
        return $this;
87
    }
88
    /**
89
     * @return string
90
     */
91
    protected function getDir(): string
92
    {
93
        return $this->dir;
94
    }
95
    /**
96
     * @param string $for
97
     * @param string $sectionName
98
     * @param string $apiName
99
     *
100
     * @return false|string
101
     * @throws \DomainException
102
     * @throws \InvalidArgumentException
103
     * @throws \LogicException
104
     */
105
    protected function getTemplateName(string $for, string $sectionName, string $apiName)
106
    {
107
        // section/api.for.twig, api.for.twig, section.for.twig, for.twig
108
        $templateNames = explode(
109
            ',',
110
            sprintf('%1$s/%2$s.%3$s.twig,%2$s.%3$s.twig,%1$s.%3$s.twig,%3$s.twig', $sectionName, $apiName, $for)
111
        );
112
        $dir = $this->getDir();
113
        foreach ($templateNames as $templateName) {
114
            if (is_file($dir . $templateName)) {
115
                return $dir . $templateName;
116
            }
117
        }
118
        return false;
119
    }
120
    /**
121
     * @return Twig_Environment
122
     */
123
    protected function getTwig(): Twig_Environment
124
    {
125
        return $this->twig;
126
    }
127
    /**
128
     * @var string $dir Directory path used when saving new files.
129
     */
130
    protected $dir;
131
    /**
132
     * Used to decide if existing file should be overwritten.
133
     *
134
     * @var bool $overwrite
135
     */
136
    protected $overwrite = false;
137
    /**
138
     * @var Twig_Environment $twig
139
     */
140
    protected $twig;
141
}
142