Completed
Push — master ( f967af...bfaf7c )
by Xinjiang
12:52
created

MongoConnection   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 4
Bugs 1 Features 3
Metric Value
c 4
b 1
f 3
dl 0
loc 95
wmc 13
lcom 1
cbo 3
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A lookup() 0 18 3
B reverseLookup() 0 24 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
    public function __construct($config)
12
    {
13
        $options = array("connectTimeoutMS" => 30000);
14
        $this->client = new \MongoDB\Client($config, $options);
15
        $this->DB = $this->client->selectDatabase("url_shortener");
16
    }
17
18
    public function lookup($slug, $update = false)
19
    {
20
        $c_db = $this->DB;
21
        $c_table = $c_db->selectCollection("url_table");
22
        $c_table->createIndex(array('shorturl' => 1 ), array('unique' => true));
23
        $urls = $c_table->findOne(array('shorturl' => $slug), array('longurl', '_id'));
24
        if ($urls) {
25
            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
            return $urls['longurl'];
32
        } else {
33
            return false;
34
        }
35
    }
36
37
    public function reverseLookup($longUrl)
38
    {
39
        // If stored, return the slug, otherwise, return false
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
40
        /*
41
         * mongodb
42
         * {
43
         *   shorturl
44
         *   longurl
45
         * }
46
         */
47
48
        $c_db = $this->DB;
49
50
        // check if url already exists
51
        $c_table = $c_db->selectCollection("url_table");
52
        $c_table->createIndex(array('longurl' => 1 ), array('unique' => true));
53
        $c_table->createIndex(array('shorturl' => 1 ), array('unique' => true));
54
        $shorturl = $c_table->findOne(array('longurl' => $longUrl), array('shorturl'));
55
        if ($shorturl && $shorturl['shorturl']) {
56
            return $shorturl['shorturl'];
57
        } else {
58
            return false;
59
        }
60
    }
61
62
    public function addNewRecord($longUrl, $slug)
63
    {
64
        $c_db = $this->DB;
65
        $c_table = $c_db->selectCollection("url_table");
66
67
        $c_table->insertOne(array("shorturl" => $slug, "longurl" => $longUrl));
68
        return $slug;
69
    }
70
71
    public function getIncrementUid()
72
    {
73
        // generate increment unique id
74
        $collection_name = 'increment_id';
75
        $unique_field =  'section';
76
        $data_field = 'url_id';
77
78
        $c_inc = $this->DB->selectCollection($collection_name);
79
        $c_inc->createIndex(array($unique_field => 1), array('unique' => true));
80
        try {
81
            $ret = $c_inc->findOneAndUpdate(
82
                [ $unique_field => $data_field ],
83
                [ '$inc' => [$data_field => 1] ],
84
                [ "upsert" => true ]
85
            );
86
            if ($ret && $ret->$data_field) {
87
                return $ret->$data_field;
88
            }
89
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class Soleo\UrlShortener\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
90
            return false;
91
        }
92
    }
93
94
    public function cleanDB()
95
    {
96
        $this->DB->selectCollection("url_table")->drop();
97
        $this->DB->selectCollection("increment_id")->drop();
98
    }
99
}
100