Completed
Push — master ( 5432fb...a44a55 )
by Peter
22:26
created

PostRepository::findOneBy()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 9.2
cc 4
eloc 11
nc 4
nop 1
1
<?php
2
3
namespace AppBundle\Repository;
4
5
use Symfony\Component\Finder\Finder;
6
use AppBundle\Entity\Post;
7
use Mni\FrontYAML\Parser;
8
9
class PostRepository
10
{
11
    /**
12
     * PostRepository constructor.
13
     *
14
     * @param string $kernelRootDir
15
     */
16
    public function __construct($kernelRootDir)
17
    {
18
        $this->path = $kernelRootDir;
0 ignored issues
show
Bug introduced by
The property path does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
19
    }
20
21
    public function findAll()
22
    {
23
        $finder = new Finder();
24
        $finder->files()->in($this->path.'/Resources/content/blog');
25
        $finder->files()->name('*.md');
26
        $finder->sort(function ($a, $b) { return strcmp($b->getRealpath(), $a->getRealpath()); });
27
28
        $posts = [];
29
        foreach ($finder as $file) {
30
            $post = $this->getPostByFile($file->getRealPath());
31
            $posts[] = $post;
32
        }
33
34
        return $posts;
35
    }
36
37
    /**
38
     * Find one Post by parameters.
39
     *
40
     * @param array $params Search parameters.
41
     * @return null|Post
42
     */
43
    public function findOneBy(array $params) {
44
        $finder = new Finder();
45
        $fileName = $params['year'].'-'.$params['month'].'-'.$params['day'].($params['num'] === 0 ? '' : '-'.$params['num']).'.md';
46
        $finder->files()->in($this->path.'/Resources/content/blog')->name($fileName);
47
        $iterator = $finder->getIterator();
48
        $iterator->rewind();
49
        $file = $iterator->current();
50
51
        $post = $this->getPostByFile($file->getRealPath());
52
53
        if ($finder->count() == 0 || $params['slug'] != $post->getSlug()) {
54
            return null;
55
        }
56
57
        return $post;
58
    }
59
60
    /**
61
     * Find limited latest posts.
62
     *
63
     * @param int $limit
64
     * @return null|array
65
     */
66
    public function findLatest($limit = 10)
67
    {
68
        $finder = new Finder();
69
        $finder->files()->in($this->path.'/Resources/content/blog');
70
        $finder->files()->name('*.md');
71
        $finder->sort(function ($a, $b) { return strcmp($b->getRealpath(), $a->getRealpath()); });
72
73
        $posts = [];
74
        foreach (new \LimitIterator($finder->getIterator(), 0, $limit) as $file) {
75
            $post = $this->getPostByFile($file->getRealPath());
76
            $posts[] = $post;
77
        }
78
79
        return $posts;
80
    }
81
82
    /**
83
     * @param string $file
84
     * @return Post|null
85
     */
86
    public function getPostByFile($file)
87
    {
88
        if (!file_exists($file)) {
89
            return null;
90
        }
91
92
        $parser = new Parser();
93
        $document = $parser->parse(file_get_contents($file));
94
95
        $post = new Post();
96
        $post->setTitle($document->getYaml()['title']);
97
        $post->setReadTime($document->getYaml()['read_time']);
98
        $post->setIntro($document->getYaml()['intro']);
99
        $post->setCreated(substr(basename($file), 10));
100
        $post->setUpdated($document->getYAML()['updated']);
101
        $post->setNum(substr(basename($file), 11, -3));
102
        $post->setSlug($document->getYaml()['slug']);
103
        $post->setContent($document->getContent());
104
105
        return $post;
106
    }
107
}
108