Completed
Push — master ( 53c3c6...647838 )
by Xinjiang
03:18
created

MongoConnection   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 81
wmc 12
lcom 1
cbo 0
rs 10

5 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
A getIncrementUid() 0 15 4
1
<?php
2
3
namespace Soleo\UrlShortener;
4
5
use \MongoClient;
6
7
class MongoConnection implements ConnectionInterface
8
{
9
10
    private $client;
11
    private $DB;
12
    public function __construct($config)
13
    {
14
        $options = array("connectTimeoutMS" => 30000);
15
        $this->client = new MongoClient($config, $options);
16
        $this->DB = $this->client->selectDB("url_shortener");
17
    }
18
19
    public function lookup($slug, $update = false)
20
    {
21
        $c_db = $this->DB;
22
        $c_table = $c_db->selectCollection("url_table");
23
        $c_table->ensureIndex(array('shorturl' => 1 ), array('unique' => true));
24
        $urls = $c_table->findOne(array('shorturl' => $slug), array('longurl', '_id'));
25
        if ($urls) {
26
            if ($update) {
27
                $c_table->ensureIndex(array('hits' => 1 ));
28
                $c_table->update(array("_id" => $urls['_id']), array('$inc' => array("hits" => 1)));
29
                $days = floor(time()/24/3600);
30
                $c_table->update(array("_id" => $urls['_id']), array('$inc' => array("hits_d.{$days}" => 1)));
31
            }
32
            return $urls['longurl'];
33
        } else {
34
            return false;
35
        }
36
    }
37
38
    public function reverseLookup($longUrl)
39
    {
40
        // 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...
41
        /*
42
         * mongodb
43
         * {
44
         *   shorturl
45
         *   longurl
46
         * }
47
         */
48
49
        $c_db = $this->DB;
50
51
        // check if url already exists
52
        $c_table = $c_db->selectCollection("url_table");
53
        $c_table->ensureIndex(array('longurl' => 1 ), array('unique' => true));
54
        $c_table->ensureIndex(array('shorturl' => 1 ), array('unique' => true));
55
        $shorturl = $c_table->findOne(array('longurl' => $longUrl), array('shorturl'));
56
        if ($shorturl && $shorturl['shorturl']) {
57
            return $shorturl['shorturl'];
58
        } else {
59
            return false;
60
        }
61
    }
62
63
    public function addNewRecord($longUrl, $slug)
64
    {
65
        $c_db = $this->DB;
66
        $c_table = $c_db->selectCollection("url_table");
67
68
        $c_table->insert(array("shorturl" => $slug, "longurl" => $longUrl));
69
        return $slug;
70
    }
71
72
    public function getIncrementUid()
73
    {
74
        // generate increment unique id
75
        $collection_name = 'increment_id';
76
        $unique_field =  'section';
77
        $data_field = 'url_id';
78
79
        $c_inc = $this->DB->selectCollection($collection_name);
80
        $c_inc->ensureIndex(array($unique_field => 1), array('unique' => true));
81
        $ret = $this->DB->command(array("findandmodify" => "increment_id", "query" => array($unique_field => $data_field),
82
                                                  "update" => array('$inc' => array($data_field => 1)), "upsert" => true ));
83
        if ($ret && $ret['value'] && $ret['value'][$data_field]) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $ret of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
84
            return $ret['value'][$data_field];
85
        }
86
    }
87
}
88