Passed
Push — Codacy ( 230dad...27b2c4 )
by Fabien
02:35
created

CurriculumVitae::isValidXmlCurriculumVitae()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.3142
cc 2
eloc 11
nc 2
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the FabienCrassat\CurriculumVitaeBundle Symfony bundle.
5
 *
6
 * (c) Fabien Crassat <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FabienCrassat\CurriculumVitaeBundle\Entity;
13
14
use FabienCrassat\CurriculumVitaeBundle\Utility\LibXmlDisplayErrors;
15
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
16
17
class CurriculumVitae extends Xml2arrayFunctions
18
{
19
    private $lang;
20
    private $curriculumVitae;
21
    private $pathToFile;
22
    private $interface;
23
    private $cvFile;
24
    private $xml2arrayFunctions;
25
26
    /**
27
     * @param string $pathToFile
28
     * @param string $lang
29
     */
30 29
    public function __construct($pathToFile, $lang = 'en') {
31 29
        $this->pathToFile = $pathToFile;
32 29
        $this->setFileName();
33 29
        $this->lang               = $lang;
34 29
        $this->curriculumVitae    = $this->getXmlCurriculumVitae();
35 26
        $this->xml2arrayFunctions = New Xml2arrayFunctions($this->curriculumVitae, $this->lang);
36 26
    }
37
38
    /**
39
     * @return null|array
40
     */
41 23
    public function getDropDownLanguages() {
42 23
        $this->interface = $this->curriculumVitae->{'langs'};
43 10
        $return          = $this->getXMLValue();
44 10
        if(!$return) {
45 1
            $return = array($this->lang => $this->lang);
46 1
        }
47
48 10
        return $return;
49
    }
50
51
    /**
52
     * @return array
53
     */
54 5
    public function getAnchors() {
55 5
        $anchorsAttribute = $this->curriculumVitae->xpath('curriculumVitae/*[attribute::anchor]');
56
57 5
        $anchors = array();
58 5
        foreach ($anchorsAttribute as $anchorsValue) {
59 5
            $anchor = (string) $anchorsValue['anchor'];
60 5
            $title  = $anchorsValue->xpath("anchorTitle[@lang='" . $this->lang . "']");
61 5
            if (count($title) == 0) {
62 1
                $title = $anchorsValue->xpath('anchorTitle');
63 1
            }
64 5
            $anchors[$anchor] = array(
65 5
                'href'  => $anchor,
66 5
                'title' => (string) $title[0],
67
            );
68 5
        };
69
70 5
        return $anchors;
71
    }
72
73
    /**
74
     * @return string
75
     */
76 5
    public function getHumanFileName() {
77 5
        $myName       = $this->getMyName();
78 5
        $myCurrentJob = $this->getMyCurrentJob();
79 5
        if (NULL != $myName) {
80 4
            if (NULL !== $myCurrentJob) {
81 3
                return $myName.' - '.$myCurrentJob;
82
            }
83
84 1
            return $myName;
85
        }
86
        
87 1
        return $this->cvFile;
88
    }
89
90
    /**
91
     * @return null|array
92
     */
93 14
    public function getIdentity() {
94 14
        $this->interface = $this->curriculumVitae->curriculumVitae->identity->items;
95 14
        return $this->getXMLValue();
96
    }
97
98
    /**
99
     * @return null|array
100
     */
101 7
    public function getFollowMe() {
102 7
        $this->interface = $this->curriculumVitae->curriculumVitae->followMe->items;
103 7
        return $this->getXMLValue();
104
    }
105
106
    /**
107
     * @return null|array
108
     */
109 12
    public function getLookingFor() {
110 12
        $this->interface = $this->curriculumVitae->curriculumVitae->lookingFor;
111 12
        return $this->getXMLValue();
112
    }
113
114
    /**
115
     * @return null|array
116
     */
117 8
    public function getExperiences() {
118 8
        $this->interface = $this->curriculumVitae->curriculumVitae->experiences->items;
119 8
        return $this->getXMLValue();
120
    }
121
122
    /**
123
     * @return null|array
124
     */
125 7
    public function getSkills() {
126 7
        $this->interface = $this->curriculumVitae->curriculumVitae->skills->items;
127 7
        return $this->getXMLValue();
128
    }
129
130
    /**
131
     * @return null|array
132
     */
133 7
    public function getEducations() {
134 7
        $this->interface = $this->curriculumVitae->curriculumVitae->educations->items;
135 7
        return $this->getXMLValue();
136
    }
137
138
    /**
139
     * @return null|array
140
     */
141 7
    public function getLanguageSkills() {
142 7
        $this->interface = $this->curriculumVitae->curriculumVitae->languageSkills->items;
143 7
        return $this->getXMLValue();
144
    }
145
146
    /**
147
     * @return null|array
148
     */
149 7
    public function getMiscellaneous() {
150 7
        $this->interface = $this->curriculumVitae->curriculumVitae->miscellaneous->items;
151 7
        return $this->getXMLValue();
152
    }
153
154 29
    private function setFileName() {
155 29
        $data = explode('/', $this->pathToFile);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
156 29
        $data = $data[count($data) - 1];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
157 29
        $data = explode('.', $data);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
158 29
        $this->cvFile = $data[0];
159 29
    }
160
161
    /**
162
     * @return string
163
     */
164 5
    private function getMyName() {
165 5
        $identity = $this->getIdentity();
166 5
        return $identity['myself']['name'];
167
    }
168
169
    /**
170
     * @return null|string
171
     */
172 5
    private function getMyCurrentJob() {
173 5
        $lookingFor = $this->getLookingFor();
174 5
        if (isset($lookingFor['experience']['job'])) {
175 3
            return $lookingFor['experience']['job'];
176 3
        } elseif (isset($lookingFor['experience'])) {
177 1
            return $lookingFor['experience'];
178
        }
179
        
180 2
        return NULL;
181
    }
182
183
    /**
184
     * @return \SimpleXMLElement
185
     */
186 29
    private function getXmlCurriculumVitae() {
187 29
        if (is_null($this->pathToFile) || !is_file($this->pathToFile)) {
188 1
            throw new InvalidArgumentException('The path ' . $this->pathToFile . ' is not a valid path to file.');
189
        }
190 28
        $this->isValidXmlCurriculumVitae();
191
192 26
        return simplexml_load_file($this->pathToFile);
193
    }
194
195
    /**
196
     * @return boolean
197
     */
198 28
    private function isValidXmlCurriculumVitae() {
199
        // Active "user error handling"
200 28
        libxml_use_internal_errors(TRUE);
201
202
        // Instanciate of a DOMDocument
203 28
        $dom = new \DOMDocument('1.0');
204
205
        // Load the XML from the file
206 28
        $dom->load($this->pathToFile);
207
208
        // Validation duof the XML document
209 28
        $reflClass = new \ReflectionClass(get_class($this));
210 28
        $xsdFile   = dirname($reflClass->getFileName()).'/validator.xsd';
211 28
        $validate  = $dom->schemaValidate($xsdFile);
212 28
        if (!$validate) {
213 2
            $libxmlDisplayErrors = new LibXmlDisplayErrors;
214 2
            throw new InvalidArgumentException($libxmlDisplayErrors->libXmlDisplayErrors());
215
        }
216
217 26
        return $validate;
218
    }
219
220
    /**
221
     * @return null|array
222
     */
223 25
    private function getXMLValue() {
224 25
        if (!$this->interface) {
225 3
            return NULL;
226
        }
227
        
228 23
        return $this->xml2arrayFunctions->xml2array($this->interface);
229
    }
230
}
231