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

ParserTrait::setConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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