Passed
Push — master ( 1227c2...8eadee )
by Ako
02:15
created
config/shorturl.php 1 patch
Indentation   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -5,25 +5,25 @@
 block discarded – undo
5 5
 			"default" => "local",
6 6
 			
7 7
 			"local" => [
8
-			    'base_url'      => env('APP_URL', '127.0.0.1'),
8
+				'base_url'      => env('APP_URL', '127.0.0.1'),
9 9
 				'table_name'    => 'links',
10
-                'charset'       => 'latin1',
10
+				'charset'       => 'latin1',
11 11
 
12
-                // utf8_bin gives us case sensitive search to the DB
13
-                'collation'     => 'latin1_bin',
12
+				// utf8_bin gives us case sensitive search to the DB
13
+				'collation'     => 'latin1_bin',
14 14
 
15
-                /**
16
-                 * For making the urls unique in the DB, we must create an index or unique_key
17
-                 * for the column url
18
-                 * But based on different versions of mysql the size of the index key
19
-                 * is different as you can see here
20
-                 * https://dev.mysql.com/doc/refman/5.7/en/innodb-restrictions.html#innodb-maximums-minimums
21
-                 *
22
-                 * Possible values in byte could be (767, 3072) and based on the
23
-                 * charset and collation used for the database or the table, given values may differ
24
-                 * for example for a ** utf8mb4 ** charset the given values could be (767/4, 3072/4);
25
-                 *
26
-                 */
15
+				/**
16
+				 * For making the urls unique in the DB, we must create an index or unique_key
17
+				 * for the column url
18
+				 * But based on different versions of mysql the size of the index key
19
+				 * is different as you can see here
20
+				 * https://dev.mysql.com/doc/refman/5.7/en/innodb-restrictions.html#innodb-maximums-minimums
21
+				 *
22
+				 * Possible values in byte could be (767, 3072) and based on the
23
+				 * charset and collation used for the database or the table, given values may differ
24
+				 * for example for a ** utf8mb4 ** charset the given values could be (767/4, 3072/4);
25
+				 *
26
+				 */
27 27
 				'index_key_prefix_size'     => '767',
28 28
 
29 29
 				/**
Please login to merge, or discard this patch.
src/ShorturlServiceProvider.php 1 patch
Indentation   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -6,26 +6,26 @@
 block discarded – undo
6 6
 
7 7
 class ShorturlServiceProvider extends ServiceProvider
8 8
 {
9
-    /**
10
-     * Register bindings in the container.
11
-     *
12
-     * @return void
13
-     */
14
-    public function register()
15
-    {
16
-        $this->app->singleton('shorturl', function ($app) {
17
-            return resolve("Ako\Shorturl\Shorturl");
18
-        });
9
+	/**
10
+	 * Register bindings in the container.
11
+	 *
12
+	 * @return void
13
+	 */
14
+	public function register()
15
+	{
16
+		$this->app->singleton('shorturl', function ($app) {
17
+			return resolve("Ako\Shorturl\Shorturl");
18
+		});
19 19
 
20
-        $this->mergeConfigFrom(__DIR__ . '/../config/shorturl.php', 'shorturl');
21
-    }
20
+		$this->mergeConfigFrom(__DIR__ . '/../config/shorturl.php', 'shorturl');
21
+	}
22 22
 
23
-    public function boot()
24
-    {
25
-        $this->publishes([
26
-            __DIR__.'/../config/shorturl.php' => config_path('shorturl.php')
27
-        ]);
23
+	public function boot()
24
+	{
25
+		$this->publishes([
26
+			__DIR__.'/../config/shorturl.php' => config_path('shorturl.php')
27
+		]);
28 28
 
29
-        $this->loadMigrationsFrom(__DIR__.'/../migrations');
30
-    }
29
+		$this->loadMigrationsFrom(__DIR__.'/../migrations');
30
+	}
31 31
 }
32 32
\ No newline at end of file
Please login to merge, or discard this patch.
src/Facades/Shorturl.php 1 patch
Indentation   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -6,14 +6,14 @@
 block discarded – undo
6 6
 
7 7
 class Shorturl extends Facade
8 8
 {
9
-    /**
10
-     * Get the registered name of the component.
11
-     *
12
-     * @return string
13
-     */
14
-    protected static function getFacadeAccessor()
15
-    {
16
-        return 'shorturl';
17
-    }
9
+	/**
10
+	 * Get the registered name of the component.
11
+	 *
12
+	 * @return string
13
+	 */
14
+	protected static function getFacadeAccessor()
15
+	{
16
+		return 'shorturl';
17
+	}
18 18
 
19 19
 }
Please login to merge, or discard this patch.
src/DriverFactory.php 1 patch
Indentation   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -1,9 +1,9 @@
 block discarded – undo
1 1
 <?php
2 2
 	namespace Ako\Shorturl;
3 3
 
4
-    use Ako\Shorturl\Drivers\LocalDriver;
4
+	use Ako\Shorturl\Drivers\LocalDriver;
5 5
 
6
-    class DriverFactory
6
+	class DriverFactory
7 7
 	{
8 8
 		/**
9 9
 		 * @param string $driver
Please login to merge, or discard this patch.
src/models/Link.php 1 patch
Indentation   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -19,8 +19,8 @@
 block discarded – undo
19 19
 		];
20 20
 
21 21
 		public function __construct(array $attributes = [])
22
-        {
23
-            $this->table = config('shorturl.drivers.local.table_name');
24
-            parent::__construct($attributes);
25
-        }
26
-    }
27 22
\ No newline at end of file
23
+		{
24
+			$this->table = config('shorturl.drivers.local.table_name');
25
+			parent::__construct($attributes);
26
+		}
27
+	}
28 28
\ No newline at end of file
Please login to merge, or discard this patch.
migrations/2019_01_02_192950_create_links_table.php 1 patch
Indentation   +25 added lines, -25 removed lines patch added patch discarded remove patch
@@ -6,31 +6,31 @@
 block discarded – undo
6 6
 
7 7
 class CreateLinksTable extends Migration
8 8
 {
9
-    /**
10
-     * Run the migrations.
11
-     */
12
-    public function up()
13
-    {
14
-        $local_conf = config('shorturl.drivers.local');
15
-        Schema::create($local_conf['table_name'], function (Blueprint $table) use ($local_conf) {
16
-            $table->charset = $local_conf['charset'] ?? "utf8";
17
-            $table->collation = $local_conf['collation'] ?? "utf8_bin";
18
-            $table->increments('id');
19
-            $table->string('long_path', $local_conf['index_key_prefix_size'])->unique();
20
-            $table->string('short_path', 10)->unique();
21
-            $table->string('base_url')->nullable();
22
-            $table->bigInteger('clicks')->nullable()->default(0);
23
-            $table->text('properties')->nullable();
24
-            $table->timestamps();
25
-        });
26
-    }
9
+	/**
10
+	 * Run the migrations.
11
+	 */
12
+	public function up()
13
+	{
14
+		$local_conf = config('shorturl.drivers.local');
15
+		Schema::create($local_conf['table_name'], function (Blueprint $table) use ($local_conf) {
16
+			$table->charset = $local_conf['charset'] ?? "utf8";
17
+			$table->collation = $local_conf['collation'] ?? "utf8_bin";
18
+			$table->increments('id');
19
+			$table->string('long_path', $local_conf['index_key_prefix_size'])->unique();
20
+			$table->string('short_path', 10)->unique();
21
+			$table->string('base_url')->nullable();
22
+			$table->bigInteger('clicks')->nullable()->default(0);
23
+			$table->text('properties')->nullable();
24
+			$table->timestamps();
25
+		});
26
+	}
27 27
 
28
-    /**
29
-     * Reverse the migrations.
30
-     */
31
-    public function down()
32
-    {
33
-        Schema::dropIfExists(config('shorturl.drivers.local.table_name'));
34
-    }
28
+	/**
29
+	 * Reverse the migrations.
30
+	 */
31
+	public function down()
32
+	{
33
+		Schema::dropIfExists(config('shorturl.drivers.local.table_name'));
34
+	}
35 35
 }
36 36
 
Please login to merge, or discard this patch.
src/drivers/LocalDriver.php 1 patch
Indentation   +120 added lines, -120 removed lines patch added patch discarded remove patch
@@ -15,84 +15,84 @@  discard block
 block discarded – undo
15 15
 			$this->main_str = $this->config['str_shuffled'];
16 16
 			$this->head = $this->main_str[0];
17 17
 			$this->tail = $this->main_str[strlen($this->main_str) - 1];
18
-            $this->checkCaseSensitive ();
18
+			$this->checkCaseSensitive ();
19 19
 		}
20 20
 
21
-        /**
22
-         * @param string $url
23
-         *
24
-         * @return string
25
-         */
21
+		/**
22
+		 * @param string $url
23
+		 *
24
+		 * @return string
25
+		 */
26 26
 		public function  expand (string $url) :string
27 27
 		{
28
-		    $this->parseUrl($url);
29
-		    $link = Link::where("short_path", $this->path)->first();
30
-		    if ($link) {
31
-		        $link->increment("clicks");
32
-		        return $link->base_url . "/" . $link->long_path;
33
-            }
28
+			$this->parseUrl($url);
29
+			$link = Link::where("short_path", $this->path)->first();
30
+			if ($link) {
31
+				$link->increment("clicks");
32
+				return $link->base_url . "/" . $link->long_path;
33
+			}
34 34
 			return "";
35 35
 		}
36 36
 
37
-        /**
38
-         * @param string $url
39
-         *
40
-         * @return string
41
-         * @throws \Exception
42
-         */
37
+		/**
38
+		 * @param string $url
39
+		 *
40
+		 * @return string
41
+		 * @throws \Exception
42
+		 */
43 43
 		public function shorten (string $url) :string
44 44
 		{
45
-            $this->parseUrl ($url);
46
-
47
-            // Check if given url has been shorten previously
48
-            $duplicate = Link::where(['long_path' => $this->path])->first();
49
-            if ($duplicate)
50
-                return $duplicate->base_url . "/" . $duplicate->short_path;
51
-
52
-            $short_path = $this->getNextShortpath();
53
-
54
-            try {
55
-                Link::create([
56
-                    "long_path" => $this->path,
57
-                    "short_path" => $short_path,
58
-                    'base_url' => $this->base_url,
59
-                    'properties' => $this->props]
60
-                );
61
-            } catch (\Exception $e) {
62
-                // If it is duplicate entry exception
63
-                // try to insert a new entry
64
-                if ($e instanceof \Illuminate\Database\QueryException && $e->getCode() == "23000") {
65
-                    return $this->shorten($url);
66
-                }
67
-            }
68
-            return $this->base_url . "/" . $short_path;
69
-        }
70
-
71
-        /**
72
-         * Git the first short url
73
-         *
74
-         * @return string
75
-         */
76
-        private function getFirstUrl () : string
77
-        {
78
-            $min_length = $this->config['min_length'];
79
-            $short_path = "";
80
-            for ($i = 0; $i < $min_length; $i++)
81
-                $short_path .= $this->head;
82
-            return $short_path;
83
-        }
84
-
85
-        /**
86
-         *
87
-         * Get the next short url based on the given item (it gets permutations one by one)
88
-         *
89
-         * @param string $current_perm
90
-         * @return string
91
-         */
92
-        private function findNextPerm (string $current_perm) :string
45
+			$this->parseUrl ($url);
46
+
47
+			// Check if given url has been shorten previously
48
+			$duplicate = Link::where(['long_path' => $this->path])->first();
49
+			if ($duplicate)
50
+				return $duplicate->base_url . "/" . $duplicate->short_path;
51
+
52
+			$short_path = $this->getNextShortpath();
53
+
54
+			try {
55
+				Link::create([
56
+					"long_path" => $this->path,
57
+					"short_path" => $short_path,
58
+					'base_url' => $this->base_url,
59
+					'properties' => $this->props]
60
+				);
61
+			} catch (\Exception $e) {
62
+				// If it is duplicate entry exception
63
+				// try to insert a new entry
64
+				if ($e instanceof \Illuminate\Database\QueryException && $e->getCode() == "23000") {
65
+					return $this->shorten($url);
66
+				}
67
+			}
68
+			return $this->base_url . "/" . $short_path;
69
+		}
70
+
71
+		/**
72
+		 * Git the first short url
73
+		 *
74
+		 * @return string
75
+		 */
76
+		private function getFirstUrl () : string
77
+		{
78
+			$min_length = $this->config['min_length'];
79
+			$short_path = "";
80
+			for ($i = 0; $i < $min_length; $i++)
81
+				$short_path .= $this->head;
82
+			return $short_path;
83
+		}
84
+
85
+		/**
86
+		 *
87
+		 * Get the next short url based on the given item (it gets permutations one by one)
88
+		 *
89
+		 * @param string $current_perm
90
+		 * @return string
91
+		 */
92
+		private function findNextPerm (string $current_perm) :string
93 93
 		{
94 94
 			if (!strlen($current_perm))
95
-			    return $this->head;
95
+				return $this->head;
96 96
 
97 97
 			$arr = array_reverse(str_split($current_perm));
98 98
 			foreach($arr as $key => $current_char) {
@@ -100,7 +100,7 @@  discard block
 block discarded – undo
100 100
 					$current_perm = Str::replaceLast($current_char, "", $current_perm);
101 101
 					return $this->findNextPerm($current_perm) . $this->head;
102 102
 				}
103
-                $next_char = str_split(Str::after($this->main_str, $current_char))[0];
103
+				$next_char = str_split(Str::after($this->main_str, $current_char))[0];
104 104
 				return Str::replaceLast($current_char, $next_char, $current_perm);
105 105
 			}
106 106
 		}
@@ -116,56 +116,56 @@  discard block
 block discarded – undo
116 116
 			return $this;
117 117
 		}
118 118
 
119
-        private function parseUrl (string $url)
120
-        {
121
-            $parse = parse_url($url);
122
-            $path = "";
123
-            if ($parse['path'] ?? null)
124
-                $path .= str::replaceFirst("/", "", $parse['path']);
125
-            if ($parse['query'] ?? null)
126
-                $path .= "?" . $parse['query'];
127
-            if ($parse['fragment'] ?? null)
128
-                $path .= "#" . $parse['fragment'];
129
-
130
-            $this->base_url = str_replace("/" . $path, "", $url);
131
-            $this->path = $path;
132
-        }
133
-
134
-        private function checkCaseSensitive ()
135
-        {
136
-            if ((!$this->config['case_sensitive'] ?? null)) {
137
-                // Remove upper cases from main string
138
-                $this->main_str = preg_replace("/(.)\\1+/", "$1", strtolower($this->main_str));
139
-            }
140
-        }
141
-
142
-        /**
143
-         * @return string
144
-         */
145
-        private function getNextShortpath () : string
146
-        {
147
-            $tbl = (new Link)->getTable();
148
-            // Get latest short_path(s)
149
-            // As multiple instances could be created at the same timestamp which we get
150
-            // the latest one based on that
151
-            // so we must find the latest one(short_path) in the permutation of the main_str
152
-            $latest = collect(\DB::select("SELECT short_path FROM $tbl WHERE created_at = (SELECT MAX(created_at) FROM $tbl)"));
153
-
154
-            if (!$latest->count())
155
-                return $this->getFirstUrl();
156
-
157
-            if ($latest->count() == 1) {
158
-                return $this->findNextPerm($latest->first()->short_path);
159
-            }
160
-            else {
161
-                foreach ($latest->reverse() as $key => $item) {
162
-                    $next = $this->findNextPerm($item->short_path);
163
-
164
-                    // If next permutation of current item is in fetched items
165
-                    // find next permutation of the next fetched one
166
-                    if (!$latest->contains("short_path", $next))
167
-                        return $next;
168
-                }
169
-            }
170
-        }
119
+		private function parseUrl (string $url)
120
+		{
121
+			$parse = parse_url($url);
122
+			$path = "";
123
+			if ($parse['path'] ?? null)
124
+				$path .= str::replaceFirst("/", "", $parse['path']);
125
+			if ($parse['query'] ?? null)
126
+				$path .= "?" . $parse['query'];
127
+			if ($parse['fragment'] ?? null)
128
+				$path .= "#" . $parse['fragment'];
129
+
130
+			$this->base_url = str_replace("/" . $path, "", $url);
131
+			$this->path = $path;
132
+		}
133
+
134
+		private function checkCaseSensitive ()
135
+		{
136
+			if ((!$this->config['case_sensitive'] ?? null)) {
137
+				// Remove upper cases from main string
138
+				$this->main_str = preg_replace("/(.)\\1+/", "$1", strtolower($this->main_str));
139
+			}
140
+		}
141
+
142
+		/**
143
+		 * @return string
144
+		 */
145
+		private function getNextShortpath () : string
146
+		{
147
+			$tbl = (new Link)->getTable();
148
+			// Get latest short_path(s)
149
+			// As multiple instances could be created at the same timestamp which we get
150
+			// the latest one based on that
151
+			// so we must find the latest one(short_path) in the permutation of the main_str
152
+			$latest = collect(\DB::select("SELECT short_path FROM $tbl WHERE created_at = (SELECT MAX(created_at) FROM $tbl)"));
153
+
154
+			if (!$latest->count())
155
+				return $this->getFirstUrl();
156
+
157
+			if ($latest->count() == 1) {
158
+				return $this->findNextPerm($latest->first()->short_path);
159
+			}
160
+			else {
161
+				foreach ($latest->reverse() as $key => $item) {
162
+					$next = $this->findNextPerm($item->short_path);
163
+
164
+					// If next permutation of current item is in fetched items
165
+					// find next permutation of the next fetched one
166
+					if (!$latest->contains("short_path", $next))
167
+						return $next;
168
+				}
169
+			}
170
+		}
171 171
 	}
172 172
\ No newline at end of file
Please login to merge, or discard this patch.