Completed
Push — master ( 520645...0aa38f )
by Antoine
03:23
created

MongoDB   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
c 2
b 0
f 1
lcom 1
cbo 4
dl 0
loc 96
ccs 0
cts 26
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A getKey() 0 4 1
A cache() 0 13 2
A isCached() 0 15 2
A flush() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Geotools library.
5
 *
6
 * (c) Antoine Corcy <[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 League\Geotools\Cache;
13
14
use League\Geotools\Batch\BatchGeocoded;
15
use League\Geotools\Exception\InvalidArgumentException;
16
use League\Geotools\Exception\RuntimeException;
17
18
/**
19
 * MongoDB cache class.
20
 *
21
 * @author Antoine Corcy <[email protected]>
22
 */
23
class MongoDB extends AbstractCache implements CacheInterface
24
{
25
    /**
26
     * The database name.
27
     *
28
     * @var string
29
     */
30
    const DATABASE = 'geotools';
31
32
    /**
33
     * The collection name.
34
     *
35
     * @var string
36
     */
37
    const COLLECTION = 'geotools_cache';
38
39
40
    /**
41
     * The collection to work with.
42
     *
43
     * @var MongoCollection
44
     */
45
    protected $collection;
46
47
48
    /**
49
     * Constructor.
50
     *
51
     * @param string $server     The server information (optional).
52
     * @param string $database   The database name (optional).
53
     * @param string $collection The collection name (optional).
54
     *
55
     * @throws InvalidArgumentException
56
     */
57
    public function __construct($server = null, $database = self::DATABASE, $collection = self::COLLECTION)
58
    {
59
        try {
60
            $mongoDB          = new \MongoClient($server);
61
            $this->collection = $mongoDB->$database->$collection;
62
        } catch (\Exception $e) {
63
            throw new InvalidArgumentException($e->getMessage());
64
        }
65
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70
    public function getKey($providerName, $query)
71
    {
72
        return md5($providerName . $query);
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78
    public function cache(BatchGeocoded $geocoded)
79
    {
80
        try {
81
            $this->collection->insert(
82
                array_merge(
83
                    array('id' => $this->getKey($geocoded->getProviderName(), $geocoded->getQuery())),
84
                    $this->normalize($geocoded)
85
                )
86
            );
87
        } catch (\Exception $e) {
88
            throw new RuntimeException($e->getMessage());
89
        }
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95
    public function isCached($providerName, $query)
96
    {
97
        $result = $this->collection->findOne(
98
            array('id' => $this->getKey($providerName, $query))
99
        );
100
101
        if (null === $result) {
102
            return false;
103
        }
104
105
        $cached = new BatchGeocoded;
106
        $cached->fromArray($result);
107
108
        return $cached;
109
    }
110
111
    /**
112
     * {@inheritDoc}
113
     */
114
    public function flush()
115
    {
116
        $this->collection->drop();
117
    }
118
}
119