MongoConnection   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 75.47%

Importance

Changes 5
Bugs 1 Features 4
Metric Value
wmc 13
c 5
b 1
f 4
lcom 1
cbo 3
dl 0
loc 94
ccs 40
cts 53
cp 0.7547
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A lookup() 0 18 3
A reverseLookup() 0 23 3
A addNewRecord() 0 8 1
B getIncrementUid() 0 22 4
A cleanDB() 0 5 1
1
<?php
2
3
namespace Soleo\UrlShortener;
4
5
class MongoConnection implements ConnectionInterface
6
{
7
8
    private $client;
9
    private $DB;
10
11 5
    public function __construct($config)
12
    {
13 5
        $options = array("connectTimeoutMS" => 30000);
14 5
        $this->client = new \MongoDB\Client($config, $options);
15 5
        $this->DB = $this->client->selectDatabase("url_shortener");
16 5
    }
17
18 1
    public function lookup($slug, $update = false)
19
    {
20 1
        $c_db = $this->DB;
21 1
        $c_table = $c_db->selectCollection("url_table");
22 1
        $c_table->createIndex(array('shorturl' => 1 ), array('unique' => true));
23 1
        $urls = $c_table->findOne(array('shorturl' => $slug), array('longurl', '_id'));
24 1
        if ($urls) {
25 1
            if ($update) {
26
                $c_table->createIndex(array('hits' => 1 ));
27
                $c_table->update(array("_id" => $urls['_id']), array('$inc' => array("hits" => 1)));
28
                $days = floor(time()/24/3600);
29
                $c_table->update(array("_id" => $urls['_id']), array('$inc' => array("hits_d.{$days}" => 1)));
30
            }
31 1
            return $urls['longurl'];
32
        } else {
33
            return false;
34
        }
35
    }
36
37 2
    public function reverseLookup($longUrl)
38
    {
39
        /*
40
         * mongodb
41
         * {
42
         *   shorturl
43
         *   longurl
44
         * }
45
         */
46
47 2
        $c_db = $this->DB;
48
49
        // check if url already exists
50 2
        $c_table = $c_db->selectCollection("url_table");
51 2
        $c_table->createIndex(array('longurl' => 1 ), array('unique' => true));
52 2
        $c_table->createIndex(array('shorturl' => 1 ), array('unique' => true));
53 2
        $shorturl = $c_table->findOne(array('longurl' => $longUrl), array('shorturl'));
54 2
        if ($shorturl && $shorturl['shorturl']) {
55 1
            return $shorturl['shorturl'];
56
        } else {
57 1
            return false;
58
        }
59
    }
60
61 1
    public function addNewRecord($longUrl, $slug)
62
    {
63 1
        $c_db = $this->DB;
64 1
        $c_table = $c_db->selectCollection("url_table");
65
66 1
        $c_table->insertOne(array("shorturl" => $slug, "longurl" => $longUrl));
67 1
        return $slug;
68
    }
69
70 1
    public function getIncrementUid()
71
    {
72
        // generate increment unique id
73 1
        $collection_name = 'increment_id';
74 1
        $unique_field =  'section';
75 1
        $data_field = 'url_id';
76
77 1
        $c_inc = $this->DB->selectCollection($collection_name);
78 1
        $c_inc->createIndex(array($unique_field => 1), array('unique' => true));
79
        try {
80 1
            $ret = $c_inc->findOneAndUpdate(
81 1
                [ $unique_field => $data_field ],
82 1
                [ '$inc' => [$data_field => 1] ],
83 1
                [ "upsert" => true ]
84 1
            );
85 1
            if ($ret && $ret->$data_field) {
86 1
                return $ret->$data_field;
87
            }
88
        } catch (\Exception $e) {
89
            return false;
90
        }
91
    }
92
93
    public function cleanDB()
94
    {
95
        $this->DB->selectCollection("url_table")->drop();
96
        $this->DB->selectCollection("increment_id")->drop();
97
    }
98
}
99