Completed
Pull Request — master (#16)
by Fabien
06:12 queued 03:13
created

CurriculumVitae::getHumanFileName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 3
eloc 6
nc 3
nop 0
crap 3
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 9
        $return          = $this->getXMLValue();
44 9
        if(!$return) {
45 1
            $return = array($this->lang => $this->lang);
46 1
        }
47
48 9
        return $return;
49
    }
50
51
    /**
52
     * @return array
53
     */
54 4
    public function getAnchors() {
55 4
        $anchorsAttribute = $this->curriculumVitae->xpath('curriculumVitae/*[attribute::anchor]');
56
57 4
        $anchors = array();
58 4
        foreach ($anchorsAttribute as $anchorsValue) {
59 4
            $anchor = (string) $anchorsValue['anchor'];
60 4
            $title  = $anchorsValue->xpath("anchorTitle[@lang='" . $this->lang . "']");
61 4
            if (count($title) == 0) {
62 1
                $title = $anchorsValue->xpath('anchorTitle');
63 1
            }
64 4
            $anchors[$anchor] = array(
65 4
                'href'  => $anchor,
66 4
                'title' => (string) $title[0],
67
            );
68 4
        };
69
70 4
        return $anchors;
71
    }
72
73
    /**
74
     * @return string
75
     */
76 5
    public function getHumanFileName() {
77 5
        $myName       = $this->getMyName();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 7 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

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

will have no issues, while

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

will report issues in lines 1 and 2.

Loading history...
78 5
        if ($myName == NULL) return $this->cvFile;
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $myName of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
79
80 4
        $myCurrentJob = $this->getMyCurrentJob();
81 4
        if ($myCurrentJob == NULL) return $myName;
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $myCurrentJob of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
82
83 3
        return $myName.' - '.$myCurrentJob;
84
    }
85
86
    /**
87
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array|null>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
88
     */
89 5
    public function getCurriculumVitaeArray() {
90
        return array(
91 5
            'identity'          => $this->getIdentity(),
92 5
            'followMe'          => $this->getFollowMe(),
93 5
            'lookingFor'        => $this->getLookingFor(),
94 5
            'experiences'       => $this->getExperiences(),
95 5
            'skills'            => $this->getSkills(),
96 5
            'educations'        => $this->getEducations(),
97 5
            'languageSkills'    => $this->getLanguageSkills(),
98 5
            'miscellaneous'     => $this->getMiscellaneous(),
99 5
        );
100
    }
101
102
    /**
103
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
104
     */
105 13
    public function getIdentity() {
106 13
        $this->interface = $this->curriculumVitae->curriculumVitae->identity->items;
107 13
        return $this->getXMLValue();
108
    }
109
110
    /**
111
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
112
     */
113 6
    public function getFollowMe() {
114 6
        $this->interface = $this->curriculumVitae->curriculumVitae->followMe->items;
115 6
        return $this->getXMLValue();
116
    }
117
118
    /**
119
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
120
     */
121 11
    public function getLookingFor() {
122 11
        $this->interface = $this->curriculumVitae->curriculumVitae->lookingFor;
123 11
        return $this->getXMLValue();
124
    }
125
126
    /**
127
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
128
     */
129 7
    public function getExperiences() {
130 7
        $this->interface = $this->curriculumVitae->curriculumVitae->experiences->items;
131 7
        return $this->getXMLValue();
132
    }
133
134
    /**
135
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
136
     */
137 6
    public function getSkills() {
138 6
        $this->interface = $this->curriculumVitae->curriculumVitae->skills->items;
139 6
        return $this->getXMLValue();
140
    }
141
142
    /**
143
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
144
     */
145 6
    public function getEducations() {
146 6
        $this->interface = $this->curriculumVitae->curriculumVitae->educations->items;
147 6
        return $this->getXMLValue();
148
    }
149
150
    /**
151
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
152
     */
153 6
    public function getLanguageSkills() {
154 6
        $this->interface = $this->curriculumVitae->curriculumVitae->languageSkills->items;
155 6
        return $this->getXMLValue();
156
    }
157
158
    /**
159
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
160
     */
161 6
    public function getMiscellaneous() {
162 6
        $this->interface = $this->curriculumVitae->curriculumVitae->miscellaneous->items;
163 6
        return $this->getXMLValue();
164
    }
165
166 29
    private function setFileName() {
167 29
        $data = explode('/', $this->pathToFile);
168 29
        $data = $data[count($data) - 1];
169 29
        $data = explode('.', $data);
170
171 29
        $this->cvFile = $data[0];
172 29
    }
173
174
    /**
175
     * @return null|string
176
     */
177 5
    private function getMyName() {
178 5
        $identity = $this->getIdentity();
179
180 5
        if (isset($identity['myself']['name'])) {
181 4
            return $identity['myself']['name'];
182
        }
183
184 1
        return NULL;
185
    }
186
187
    /**
188
     * @return null|string
189
     */
190 4
    private function getMyCurrentJob() {
191 4
        $lookingFor = $this->getLookingFor();
192 4
        if (isset($lookingFor['experience']['job'])) {
193 3
            return $lookingFor['experience']['job'];
194 2
        } elseif (isset($lookingFor['experience'])) {
195 1
            return $lookingFor['experience'];
196
        }
197
198 1
        return NULL;
199
    }
200
201
    /**
202
     * @return \SimpleXMLElement
203
     */
204 29
    private function getXmlCurriculumVitae() {
205 29
        if (is_null($this->pathToFile) || !is_file($this->pathToFile)) {
206 1
            throw new InvalidArgumentException('The path ' . $this->pathToFile . ' is not a valid path to file.');
207
        }
208 28
        $this->isValidXmlCurriculumVitae();
209
210 26
        return simplexml_load_file($this->pathToFile);
211
    }
212
213
    /**
214
     * @return boolean
215
     */
216 28
    private function isValidXmlCurriculumVitae() {
217
        // Active "user error handling"
218 28
        libxml_use_internal_errors(TRUE);
219
220
        // Instanciate of a DOMDocument
221 28
        $dom = new \DOMDocument('1.0');
222
223
        // Load the XML from the file
224 28
        $dom->load($this->pathToFile);
225
226
        // Validation duof the XML document
227 28
        $reflClass = new \ReflectionClass(get_class($this));
228 28
        $xsdFile   = dirname($reflClass->getFileName()).'/validator.xsd';
229 28
        $validate  = $dom->schemaValidate($xsdFile);
230 28
        if (!$validate) {
231 2
            $libxmlDisplayErrors = new LibXmlDisplayErrors;
232 2
            throw new InvalidArgumentException($libxmlDisplayErrors->libXmlDisplayErrors());
233
        }
234
235 26
        return $validate;
236
    }
237
238
    /**
239
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
240
     */
241 25
    private function getXMLValue() {
242 25
        if (!$this->interface) {
243 3
            return array();
244
        }
245
246 23
        return $this->xml2arrayFunctions->xml2array($this->interface);
247
    }
248
}
249