IpManager::createIp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
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\IpFilterBundle\Model;
13
14
use Symfony\Bridge\Doctrine\RegistryInterface;
15
16
class IpManager implements IpManagerInterface
17
{
18
    /**
19
     * @var \Doctrine\Common\Persistence\ObjectManager
20
     */
21
    protected $entity_manager;
22
23
    /**
24
     * @var string
25
     */
26
    protected $class;
27
28
    /**
29
     * @var \Doctrine\Common\Persistence\ObjectRepository
30
     */
31
    protected $entity_repository;
32
33
    public function __construct(RegistryInterface $registry, $class)
34
    {
35
        $this->class = $class;
36
        $this->entity_manager = $registry->getManagerForClass($class);
37
        $this->entity_repository = $this->entity_manager->getRepository($class);
38
    }
39
40
    /**
41
     * @return \Doctrine\Common\Persistence\ObjectManager
42
     */
43
    protected function getEntityManager()
44
    {
45
        return $this->entity_manager;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    protected function getEntityRepository()
52
    {
53
        return $this->entity_repository;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function findIpAddress($ip, $environment)
60
    {
61
        return $this->getEntityRepository()->createQueryBuilder('r')
62
            ->where('r.ip = :ip')
63
            ->andWhere("r.environment LIKE :environment OR r.environment='a:0:{}'")
64
            ->orderBy('r.authorized', 'DESC')
65
            ->setParameter('ip', $ip)
66
            ->setParameter('environment', "%$environment%")
67
            ->getQuery()
68
            ->execute();
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function createIp()
75
    {
76
        $class = $this->class;
77
78
        return new $class();
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function saveIp(IpInterface $ip)
85
    {
86
        $this->getEntityManager()->persist($ip);
87
        $this->getEntityManager()->flush();
88
89
        return $this;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function deleteIp(IpInterface $ip)
96
    {
97
        $this->getEntityManager()->remove($ip);
98
        $this->getEntityManager()->flush();
99
100
        return $this;
101
    }
102
}
103