HoneypotManager::isFileEnabled()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the EoHoneypotBundle package.
5
 *
6
 * (c) Eymen Gunay <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Eo\HoneypotBundle\Manager;
13
14
use Eo\HoneypotBundle\Model\HoneypotPrey;
15
use Eo\HoneypotBundle\Model\HoneypotPreyInterface;
16
use Doctrine\Common\Persistence\ObjectManager;
17
18
class HoneypotManager
19
{
20
    /**
21
     * @var array
22
     */
23
    protected $options;
24
    
25
    /**
26
     * @var ObjectManager
27
     */
28
    protected $om;
29
30
    /**
31
     * Class constructor
32
     *
33
     * @param array $options
34
     */
35
    public function __construct(array $options)
36
    {
37
        $this->options = $options;
38
    }
39
40
    /**
41
     * Is blacklisted
42
     *
43
     * @param  string  $ip
44
     * @return boolean
45
     */
46
    public function isBlacklisted($ip)
47
    {
48
        if (!$this->isDbEnabled()) {
49
            throw new \LogicException("Can not check ip without database storage");
50
        }
51
        return count($this->getRepository()->findBy(array('ip' => $ip))) > 0;
52
    }
53
54
    /**
55
     * Create new
56
     *
57
     * @param  string $ip
58
     * @return HoneypotPreyInterface
59
     */
60
    public function createNew($ip)
61
    {
62
        if (!$this->isDbEnabled()) {
63
            $prey = new HoneypotPrey($ip);
64
        } else {
65
            $class = $this->getRepository()->getClassName();
66
            $prey = new $class($ip);
67
        }
68
        return $prey;
69
    }
70
71
    /**
72
     * Save prey
73
     *
74
     * @param HoneypotPreyInterface $prey
75
     */
76
    public function save(HoneypotPreyInterface $prey)
77
    {
78
        if ($this->isDbEnabled()) {
79
            $this->getObjectManager()->persist($prey);
80
            $this->getObjectManager()->flush($prey);
81
        }
82
83
        if ($this->isFileEnabled()) {
84
            $data = sprintf("[%s] - %s\n", $prey->getCreatedAt()->format('c'), $prey->getIp());
85
            file_put_contents($this->options['storage']['file']['output'], $data, FILE_APPEND);
86
        }
87
    }
88
89
    /**
90
     * Set object manager
91
     *
92
     * @param  ObjectManager $om
93
     * @return self
94
     */
95
    public function setObjectManager(ObjectManager $om)
96
    {
97
        $this->om = $om;
98
        return $this;
99
    }
100
101
    /**
102
     * Get object manager
103
     *
104
     * @return ObjectManager
105
     */
106
    public function getObjectManager()
107
    {
108
        return $this->om;
109
    }
110
111
    /**
112
     * Get object manager
113
     *
114
     * @return ObjectRepository
115
     */
116
    public function getRepository()
117
    {
118
        return $this->getObjectManager()->getRepository($this->options['storage']['database']['class']);
119
    }
120
121
    /**
122
     * Is db enabled
123
     *
124
     * @return boolean
125
     */
126
    public function isDbEnabled()
127
    {
128
        return $this->options['storage']['database']['enabled'];
129
    }
130
131
    /**
132
     * Is file enabled
133
     *
134
     * @return boolean
135
     */
136
    public function isFileEnabled()
137
    {
138
        return $this->options['storage']['file']['enabled'];
139
    }
140
141
    /**
142
     * Get options
143
     *
144
     * @return array
145
     */
146
    public function getOptions()
147
    {
148
        return $this->options;
149
    }
150
}
151