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

MongoConnection::cleanDB()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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