DoctrineThoughtsFeed::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Donut Social Network - Yet another experimental social network.
5
 * Copyright (C) 2016-2017, Dejan Angelov <[email protected]>
6
 *
7
 * This file is part of Donut Social Network.
8
 *
9
 * Donut Social Network is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Donut Social Network is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Donut Social Network.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * @package Donut Social Network
23
 * @copyright Copyright (C) 2016-2017, Dejan Angelov <[email protected]>
24
 * @license https://github.com/angelov/donut/blob/master/LICENSE
25
 * @author Dejan Angelov <[email protected]>
26
 */
27
28
namespace Angelov\Donut\Thoughts\ThoughtsFeed;
29
30
use Doctrine\ORM\EntityManagerInterface;
31
use Doctrine\ORM\Query;
32
use Angelov\Donut\Core\ResultLists\AbstractDoctrineResultsList;
33
use Angelov\Donut\Thoughts\Thought;
34
use Angelov\Donut\Thoughts\ThoughtsFeed\ThoughtsFeedInterface;
35
use Angelov\Donut\Users\User;
36
37
class DoctrineThoughtsFeed extends AbstractDoctrineResultsList implements ThoughtsFeedInterface
38
{
39
    private $em;
40
    private $currentUser;
41
42
    private $includeOwnThoughts = true;
43
    private $filterSource;
44
45
    public function __construct(EntityManagerInterface $entityManager, User $currentUser)
46
    {
47
        $this->em = $entityManager;
48
        $this->currentUser = $currentUser;
49
    }
50
51
    public function filterSource(int $source) : void
52
    {
53
        $this->filterSource = $source;
54
    }
55
56
    public function includeOwnThoughts(bool $includeOwn = true) : void
57
    {
58
        $this->includeOwnThoughts = $includeOwn;
59
    }
60
61
    protected function prepareQuery() : Query
62
    {
63
        $queryBuilder = $this->em->createQueryBuilder()
64
            ->select('thought')
65
            ->from(Thought::class, 'thought')
66
            ->join('thought.author', 'author');
67
68
        switch ($this->filterSource) {
69
            case ThoughtsFeedInterface::FROM_FRIENDS:
70
                $queryBuilder
71
                    ->where('author in (:friends)')
72
                    ->setParameter('friends', $this->currentUser->getFriends());
73
                break;
74
        }
75
76
        if ($this->includeOwnThoughts && $this->filterSource === ThoughtsFeedInterface::FROM_FRIENDS) {
77
            $queryBuilder
78
                ->orWhere('author = :current')
79
                ->setParameter('current', $this->currentUser);
80
        }
81
82
        foreach ($this->getOrderFields() as $orderField) {
83
            $queryBuilder->addOrderBy($orderField->getField(), $orderField->getDirection());
84
        }
85
86
        return $queryBuilder->getQuery();
87
    }
88
}
89