PoolingController::getLink()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 9
nc 3
nop 2
crap 3
1
<?php
2
3
/**
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2015 Repo2
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in all
16
 * copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
 * SOFTWARE.
25
 */
26
27
namespace Repo2\QueryReactor\Controller;
28
29
use Repo2\QueryReactor\Controller;
30
use Repo2\QueryReactor\Driver;
31
use Repo2\QueryReactor\Query;
32
33
class PoolingController implements Controller
34
{
35
    /** @var array */
36
    private $params;
37
38
    /** @var \SplObjectStorage */
39
    private $pool;
40
41
    /** @var \SplQueue */
42
    private $idle;
43
44
    /** @var \SplQueue */
45
    private $waiting;
46
47
    /**
48
     * @param array $params
49
     */
50 48
    public function __construct(array $params)
51
    {
52 48
        $this->params = $params + [
53 48
            'max_connections' => 10,
54 48
            'username' => null,
55
            'passwd' => null
56 48
        ];
57 48
        $this->pool = new \SplObjectStorage();
58 48
        $this->idle = new \SplQueue();
59 48
        $this->waiting = new \SplQueue();
60 48
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65 48
    public function getLink(Driver $driver, Query $query)
66
    {
67 48
        if (!$this->idle->isEmpty()) {
68 48
            return $this->idle->dequeue();
69
        }
70 48
        if ($this->pool->count() >= $this->params['max_connections']) {
71 6
            $this->waiting->enqueue($query);
72 6
            return false;
73
        }
74 48
        $link = $driver->connect($this->params, $this->params['username'], $this->params['passwd']);
75 48
        $this->pool->attach($link);
76 48
        return $link;
77
    }
78
79
    /**
80
     * @inheritDoc
81
     */
82 48
    public function getQuery(Driver $driver, $link)
83
    {
84 48
        if (!$this->pool->contains($link)) {
85 3
            throw new \OutOfBoundsException(sprintf('Undefined %s in the pooling controller.', $driver->info($link)));
86
        }
87 48
        if (!$this->waiting->isEmpty()) {
88 6
            return $this->waiting->dequeue();
89
        }
90 48
        $this->idle->enqueue($link);
91 48
    }
92
}
93