SequenceRepository::exists()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace BWC\Share\Data\Sequence;
4
5
use Doctrine\DBAL\Connection;
6
7
abstract class SequenceRepository implements SequenceRepositoryInterface
8
{
9
    const MAX_UNIQUE_LOOPS = 20;
10
11
    /** @var \Doctrine\DBAL\Connection */
12
    private $_con;
13
14
    /** @var \BWC\Share\Data\Sequence\SequenceGeneratorInterface */
15
    private $_generator;
16
17
18
19
    function __construct(Connection $connection, SequenceGeneratorInterface $generator) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
20
        $this->_con = $connection;
21
        $this->_generator = $generator;
22
    }
23
24
25
26
    abstract function getTableName();
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
27
28
29
    /**
30
     * @param string $name
31
     * @return string
32
     */
33
    function generate($name = 'default') {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
34
        $result = null;
35
        for ($i=0; $i<self::MAX_UNIQUE_LOOPS; $i++) {
36
            $value = $this->_generator->generate();
37
            try {
38
                $this->_con->insert($this->getTableName(), array('name'=>$name, 'value'=>$value));
39
                $result = $value;
40
                break;
41
            } catch (\Exception $ex) { }
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
42
        }
43
        return $result;
44
    }
45
46
47
    /**
48
     * @param string $value
49
     * @param string $name
50
     * @return bool
51
     */
52
    function exists($value, $name = 'default') {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
53
        $tbl = $this->getTableName();
54
        $count = $this->_con->fetchColumn("SELECT count(*) FROM `$tbl` WHERE name=:name AND value=:value",
55
            array(':name'=>$name, ':value'=>$value)
56
        );
57
        return $count ? true : false;
58
    }
59
60
}