getVisitorCountForViewReference()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace Victoire\Bundle\AnalyticsBundle\Helper;
4
5
use Doctrine\ORM\EntityManager;
6
use Victoire\Bundle\BlogBundle\Entity\Article;
7
use Victoire\Bundle\PageBundle\Helper\PageHelper;
8
use Victoire\Bundle\ViewReferenceBundle\Connector\ViewReferenceRepository;
9
10
/**
11
 * Analytics View helper
12
 * ref: victoire_analytics.view_helper.
13
 */
14
class AnalyticsViewHelper
15
{
16
    protected $viewReferenceRepository;
17
    protected $entityManager;
18
    protected $pageHelper;
19
20
    /**
21
     * AnalyticsViewHelper constructor.
22
     *
23
     * @param ViewReferenceRepository $viewReferenceRepository
24
     * @param EntityManager           $entityManager
25
     * @param PageHelper              $pageHelper
26
     */
27
    public function __construct(ViewReferenceRepository $viewReferenceRepository, EntityManager $entityManager, PageHelper $pageHelper)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
28
    {
29
        $this->entityManager = $entityManager;
30
        $this->viewReferenceRepository = $viewReferenceRepository;
31
        $this->pageHelper = $pageHelper;
32
    }
33
34
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$viewNamespace" missing
Loading history...
introduced by
Doc comment for parameter "$number" missing
Loading history...
35
     * Get the most read views by type.
36
     *
37
     * @return View[]
38
     **/
39
    public function getMostReadByViewType($viewNamespace, $number)
40
    {
41
        $views = [];
42
43
        switch ($viewNamespace) {
44
            case 'Victoire\Bundle\PageBundle\Entity\Page':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
45
46
                $viewReferences = [];
47
                $repo = $this->entityManager->getRepository($viewNamespace);
48
                //get pages and viewReferenceIds
49
                foreach ($repo->getAll()->run() as $key => $page) {
50
                    $viewReference = $this->viewReferenceRepository->getOneReferenceByParameters(
51
                        [
52
                            'viewNamespace' => $viewNamespace,
53
                            'viewId'        => $page->getId(),
54
                        ]
55
                    );
56
                    $viewReferences[$viewReference->getId()] = $viewReference;
57
                }
58
                //get pager
59
                $browseEvents = $this->entityManager->getRepository('Victoire\Bundle\AnalyticsBundle\Entity\BrowseEvent')
60
                    ->getMostVisitedFromReferences(array_keys($viewReferences), $number)
61
                    ->getQuery()
62
                    ->getResult();
63
                //Now we get the most visited references, we'll get views with PageHelper
64
                foreach ($browseEvents as $browseEvent) {
65
                    $views[] = $this->pageHelper->findPageByReference(
66
                        $viewReferences[$browseEvent->getViewReferenceId()]
67
                    );
68
                }
69
70
                break;
71
72
            default:
73
                // code...
74
                break;
75
        }
76
77
        return $views;
78
    }
79
80
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$blog" missing
Loading history...
introduced by
Doc comment for parameter "$number" missing
Loading history...
introduced by
Doc comment for parameter "$excludeUnpublished" missing
Loading history...
81
     * Get the most read articles by blog.
82
     *
83
     * @return Article[]
84
     **/
85
    public function getMostReadArticlesByBlog($blog, $number, $excludeUnpublished = true)
86
    {
87
        $viewReferences = [];
88
        //get articles and viewReferenceIds
89
        $articles = $this->entityManager->getRepository('Victoire\Bundle\BlogBundle\Entity\Article')
90
                    ->getAll($excludeUnpublished)
91
                    ->filterByBlog($blog)
92
                    ->run();
93
94
        foreach ($articles as $key => $article) {
95
            if ($viewReference = $this->viewReferenceRepository->getOneReferenceByParameters(
96
                [
97
                    'entityNamespace' => 'Victoire\Bundle\BlogBundle\Entity\Article',
98
                    'entityId'        => $article->getId(),
99
                ]
100
            )) {
101
                $viewReferences[$viewReference->getId()] = $viewReference;
102
            }
103
        }
104
        //get pager
105
        $browseEvents = $this->entityManager->getRepository('Victoire\Bundle\AnalyticsBundle\Entity\BrowseEvent')
106
            ->getMostVisitedFromReferences(array_keys($viewReferences), $number)
107
            ->getQuery()
108
            ->getResult();
109
110
        $views = [];
111
        //Now we get the most visited references, we'll get views with PageHelper
112
        foreach ($browseEvents as $browseEvent) {
113
            $views[] = $this->pageHelper->findPageByReference(
114
                $viewReferences[$browseEvent->getViewReferenceId()]
115
            );
116
        }
117
118
        return $views;
119
    }
120
121
    /**
122
     * Get number of unique visitor for a viewReference.
123
     *
124
     * @param string $viewReferenceId
125
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
126
    public function getVisitorCountForViewReference($viewReferenceId)
127
    {
128
        $viewCount = $this->entityManager->getRepository('Victoire\Bundle\AnalyticsBundle\Entity\BrowseEvent')
129
            ->getNumberOfEventForViewReferenceId($viewReferenceId)
130
            ->getQuery()
131
            ->getSingleScalarResult();
132
133
        return $viewCount;
134
    }
135
}
136