Completed
Push — master ( d97c7a...19a358 )
by Mehmet
03:14 queued 01:12
created

ParserTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 48
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setConfig() 0 5 1
A getConfigFromFile() 0 12 2
A isConfigEmpty() 0 6 2
1
<?php
2
3
namespace Selami\Entity\Parser;
4
5
use Selami\Entity\Exception\FileNotFoundException;
6
use InvalidArgumentException;
7
8
trait ParserTrait
9
{
10
    protected $schemaConfig;
11
12
    /**
13
     * Set config string
14
     * @param string $configString
15
     * @return mixed
16
     * @throws InvalidArgumentException
17
     */
18
    public function setConfig(string $configString)
19
    {
20
        $this->isConfigEmpty($configString);
21
        $this->schemaConfig = $configString;
22
    }
23
24
    /**
25
     * Set the file contains config string
26
     * @param string $configFile
27
     * @return mixed
28
     * @throws FileNotFoundException
29
     * @throws InvalidArgumentException
30
     */
31
    public function getConfigFromFile(string $configFile)
32
    {
33
        if (!file_exists($configFile)) {
34
            $message = sprintf(
35
                'File %s could be found',
36
                $configFile
37
            );
38
            throw new FileNotFoundException($message);
39
        }
40
        $configString = file_get_contents($configFile);
41
        $this->setConfig($configString);
42
    }
43
44
    /**
45
     * Checks if schemaConfig is empty
46
     * @param string $configString
47
     * @throws InvalidArgumentException
48
     */
49
    protected function isConfigEmpty(string $configString)
50
    {
51
        if (empty($configString)) {
52
            throw new InvalidArgumentException('Schema config can\'t be empty');
53
        }
54
    }
55
}
56