Completed
Push — master ( d86da0...111b33 )
by Florent
04:14
created

JotManager::deleteExpired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4286
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2015 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace SpomkyLabs\JoseBundle\Model;
13
14
use Doctrine\Common\Persistence\ManagerRegistry;
15
16
class JotManager implements JotManagerInterface
17
{
18
    /**
19
     * @var \Doctrine\Common\Persistence\ManagerRegistry
20
     */
21
    private $manager_registry = null;
22
23
    /**
24
     * @var \Doctrine\Common\Persistence\ObjectRepository|null
25
     */
26
    private $entity_repository = null;
27
28
    /**
29
     * @var \Doctrine\Common\Persistence\ObjectManager|null
30
     */
31
    private $entity_manager = null;
32
33
    /**
34
     * @var string
35
     */
36
    private $class;
37
38
    /**
39
     * @param                                              $class
40
     * @param \Doctrine\Common\Persistence\ManagerRegistry $manager_registry
41
     */
42
    public function __construct($class, ManagerRegistry $manager_registry)
43
    {
44
        $this->setClass($class);
45
        $this->setManagerRegistry($manager_registry);
46
    }
47
48
    /**
49
     * @param \Doctrine\Common\Persistence\ManagerRegistry $manager_registry
50
     *
51
     * @return $this
52
     */
53
    private function setManagerRegistry(ManagerRegistry $manager_registry)
54
    {
55
        $this->manager_registry = $manager_registry;
56
57
        return $this;
58
    }
59
60
    /**
61
     * @return \Doctrine\Common\Persistence\ManagerRegistry
62
     */
63
    private function getManagerRegistry()
64
    {
65
        return $this->manager_registry;
66
    }
67
68
    /**
69
     * @param string $class
70
     *
71
     * @return self
72
     */
73
    private function setClass($class)
74
    {
75
        $this->class = $class;
76
77
        return $this;
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    private function getClass()
84
    {
85
        return $this->class;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function getEntityManager()
92
    {
93
        if (null === $this->entity_manager) {
94
            $this->entity_manager = $this->getManagerRegistry()->getManagerForClass($this->getClass());
95
        }
96
97
        return $this->entity_manager;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function getEntityRepository()
104
    {
105
        if (null === $this->entity_repository) {
106
            $this->entity_repository = $this->getEntityManager()->getRepository($this->getClass());
107
        }
108
109
        return $this->entity_repository;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function createJot()
116
    {
117
        $class = $this->getClass();
118
119
        return new $class();
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function saveJot(JotInterface $jot)
126
    {
127
        $this->getEntityManager()->persist($jot);
128
        $this->getEntityManager()->flush();
129
130
        return $this;
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function removeJot(JotInterface $jot)
137
    {
138
        $this->getEntityManager()->remove($jot);
139
        $this->getEntityManager()->flush();
140
141
        return $this;
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function getJotById($jti)
148
    {
149
        return $this->getEntityRepository()->findOneBy(['jti' => $jti]);
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    public function deleteExpired()
156
    {
157
        $qb = $this->getEntityRepository()->createQueryBuilder('t');
158
        $qb
159
            ->delete()
160
            ->where('t.expires_at < :now')
161
            ->andWhere('t.expires_at IS NOT NULL')
162
            ->setParameters(['now' => time()]);
163
164
        return $qb->getQuery()->execute();
165
    }
166
}
167