PsrRepository::findOneBySlug()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
3
namespace AppBundle\Repository;
4
5
use Symfony\Component\Finder\Finder;
6
use AppBundle\Entity\Psr;
7
use Mni\FrontYAML\Parser;
8
9
class PsrRepository
10
{
11
    /**
12
     * @var string
13
     */
14
    private $path;
15
16
    /**
17
     * @var Parser
18
     */
19
    private $parser;
20
21
    /**
22
     * PsrRepository constructor.
23
     *
24
     * @param $kernelProjectDir
25
     * @param Parser $parser
26
     */
27
    public function __construct($kernelProjectDir, Parser $parser)
28
    {
29
        $this->path = $kernelProjectDir;
30
        $this->parser = $parser;
31
    }
32
33
    /**
34
     * return array
35
     */
36
    public function findAll()
37
    {
38
        $finder = new Finder();
39
        $finder->files()->in($this->path.'/vendor/symfony-si/fig-standards-sl/accepted');
40
        $finder->files()->name('*md');
41
        $finder->sortByName();
42
43
        $psrs = [];
44
        foreach ($finder as $file) {
45
            if (substr($file->getFileName(), -7) != 'meta.md' && substr($file->getFileName(), -11) != 'examples.md') {
46
                $psrs[] = $this->getPsrByFile($file->getRealPath());
47
            }
48
        }
49
50
        return $psrs;
51
    }
52
53
    /**
54
     * @param string $slug
55
     * @return Psr
56
     */
57
    public function findOneBySlug($slug)
58
    {
59
        $finder = new Finder();
60
        $finder->files()->in($this->path.'/vendor/symfony-si/fig-standards-sl/accepted')->name("*.md");
61
        $finder->sortByName();
62
63
        foreach ($finder as $file) {
64
            $document = $this->parser->parse($file->getContents());
65
            if ($document->getYAML()['slug'] == $slug) {
66
                return $this->getPsrByFile($file->getRealPath());
67
            }
68
        }
69
70
        return null;
71
    }
72
73
    /**
74
     * @param string $file
75
     * @return null|Psr
76
     */
77
    public function getPsrByFile($file)
78
    {
79
        if (!file_exists($file)) {
80
            return null;
81
        }
82
83
        $psr = $this->getPsrByFileWithoutChilds($file);
84
        $metaFile = substr($file, 0, -3).'-meta.md';
85
        if (file_exists($metaFile)) {
86
            $meta = $this->getPsrByFileWithoutChilds($metaFile);
87
            $psr->setMeta($meta);
88
        }
89
90
        $examplesFile = substr($file, 0, -3).'-examples.md';
91
        if (file_exists($examplesFile)) {
92
            $examples = $this->getPsrByFileWithoutChilds($examplesFile);
93
            $psr->setExamples($examples);
94
        }
95
96
        return $psr;
97
    }
98
99
    /**
100
     * Get Psr without meta or example child documents.
101
     *
102
     * @param string $file
103
     * @return Psr
104
     */
105
    private function getPsrByFileWithoutChilds($file)
106
    {
107
        $document = $this->parser->parse(file_get_contents($file));
108
109
        $psr = new Psr();
110
        $psr->setTitle($document->getYAML()['title']);
111
        $psr->setSlug($document->getYaml()['slug']);
112
        $psr->setContent($document->getContent());
113
        $psr->setReadTime($document->getYAML()['read_time']);
114
        $psr->setDescription($document->getYAML()['description']);
115
        $psr->setUpdated(\DateTime::createFromFormat('Y-m-d', $document->getYAML()['updated']));
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor...->getYAML()['updated']) targeting DateTime::createFromFormat() can also be of type false; however, AppBundle\Entity\Psr::setUpdated() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
116
117
        return $psr;
118
    }
119
}
120