@@ -15,84 +15,84 @@ discard block |
||
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 |
||
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 |
||
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 |
@@ -9,13 +9,13 @@ discard block |
||
9 | 9 | protected $props = []; |
10 | 10 | protected $config, $main_str, $head, $tail, $base_url, $path; |
11 | 11 | |
12 | - public function __construct () |
|
12 | + public function __construct() |
|
13 | 13 | { |
14 | 14 | $this->config = config('shorturl.drivers.local'); |
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 | 21 | /** |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | * |
24 | 24 | * @return string |
25 | 25 | */ |
26 | - public function expand (string $url) :string |
|
26 | + public function expand(string $url) :string |
|
27 | 27 | { |
28 | 28 | $this->parseUrl($url); |
29 | 29 | $link = Link::where("short_path", $this->path)->first(); |
@@ -40,9 +40,9 @@ discard block |
||
40 | 40 | * @return string |
41 | 41 | * @throws \Exception |
42 | 42 | */ |
43 | - public function shorten (string $url) :string |
|
43 | + public function shorten(string $url) :string |
|
44 | 44 | { |
45 | - $this->parseUrl ($url); |
|
45 | + $this->parseUrl($url); |
|
46 | 46 | |
47 | 47 | // Check if given url has been shorten previously |
48 | 48 | $duplicate = Link::where(['long_path' => $this->path])->first(); |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | * |
74 | 74 | * @return string |
75 | 75 | */ |
76 | - private function getFirstUrl () : string |
|
76 | + private function getFirstUrl() : string |
|
77 | 77 | { |
78 | 78 | $min_length = $this->config['min_length']; |
79 | 79 | $short_path = ""; |
@@ -89,13 +89,13 @@ discard block |
||
89 | 89 | * @param string $current_perm |
90 | 90 | * @return string |
91 | 91 | */ |
92 | - private function findNextPerm (string $current_perm) :string |
|
92 | + private function findNextPerm(string $current_perm) :string |
|
93 | 93 | { |
94 | 94 | if (!strlen($current_perm)) |
95 | 95 | return $this->head; |
96 | 96 | |
97 | 97 | $arr = array_reverse(str_split($current_perm)); |
98 | - foreach($arr as $key => $current_char) { |
|
98 | + foreach ($arr as $key => $current_char) { |
|
99 | 99 | if ($current_char == $this->tail) { |
100 | 100 | $current_perm = Str::replaceLast($current_char, "", $current_perm); |
101 | 101 | return $this->findNextPerm($current_perm) . $this->head; |
@@ -110,13 +110,13 @@ discard block |
||
110 | 110 | * |
111 | 111 | * @return LocalDriver |
112 | 112 | */ |
113 | - public function withProperties (array $props = []) :LocalDriver |
|
113 | + public function withProperties(array $props = []) :LocalDriver |
|
114 | 114 | { |
115 | 115 | $this->props = array_merge($this->props, $props); |
116 | 116 | return $this; |
117 | 117 | } |
118 | 118 | |
119 | - private function parseUrl (string $url) |
|
119 | + private function parseUrl(string $url) |
|
120 | 120 | { |
121 | 121 | $parse = parse_url($url); |
122 | 122 | $path = ""; |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | $this->path = $path; |
132 | 132 | } |
133 | 133 | |
134 | - private function checkCaseSensitive () |
|
134 | + private function checkCaseSensitive() |
|
135 | 135 | { |
136 | 136 | if ((!$this->config['case_sensitive'] ?? null)) { |
137 | 137 | // Remove upper cases from main string |
@@ -142,7 +142,7 @@ discard block |
||
142 | 142 | /** |
143 | 143 | * @return string |
144 | 144 | */ |
145 | - private function getNextShortpath () : string |
|
145 | + private function getNextShortpath() : string |
|
146 | 146 | { |
147 | 147 | $tbl = (new Link)->getTable(); |
148 | 148 | // Get latest short_path(s) |
@@ -46,8 +46,9 @@ discard block |
||
46 | 46 | |
47 | 47 | // Check if given url has been shorten previously |
48 | 48 | $duplicate = Link::where(['long_path' => $this->path])->first(); |
49 | - if ($duplicate) |
|
50 | - return $duplicate->base_url . "/" . $duplicate->short_path; |
|
49 | + if ($duplicate) { |
|
50 | + return $duplicate->base_url . "/" . $duplicate->short_path; |
|
51 | + } |
|
51 | 52 | |
52 | 53 | $short_path = $this->getNextShortpath(); |
53 | 54 | |
@@ -77,8 +78,9 @@ discard block |
||
77 | 78 | { |
78 | 79 | $min_length = $this->config['min_length']; |
79 | 80 | $short_path = ""; |
80 | - for ($i = 0; $i < $min_length; $i++) |
|
81 | - $short_path .= $this->head; |
|
81 | + for ($i = 0; $i < $min_length; $i++) { |
|
82 | + $short_path .= $this->head; |
|
83 | + } |
|
82 | 84 | return $short_path; |
83 | 85 | } |
84 | 86 | |
@@ -91,8 +93,9 @@ discard block |
||
91 | 93 | */ |
92 | 94 | private function findNextPerm (string $current_perm) :string |
93 | 95 | { |
94 | - if (!strlen($current_perm)) |
|
95 | - return $this->head; |
|
96 | + if (!strlen($current_perm)) { |
|
97 | + return $this->head; |
|
98 | + } |
|
96 | 99 | |
97 | 100 | $arr = array_reverse(str_split($current_perm)); |
98 | 101 | foreach($arr as $key => $current_char) { |
@@ -120,12 +123,15 @@ discard block |
||
120 | 123 | { |
121 | 124 | $parse = parse_url($url); |
122 | 125 | $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']; |
|
126 | + if ($parse['path'] ?? null) { |
|
127 | + $path .= str::replaceFirst("/", "", $parse['path']); |
|
128 | + } |
|
129 | + if ($parse['query'] ?? null) { |
|
130 | + $path .= "?" . $parse['query']; |
|
131 | + } |
|
132 | + if ($parse['fragment'] ?? null) { |
|
133 | + $path .= "#" . $parse['fragment']; |
|
134 | + } |
|
129 | 135 | |
130 | 136 | $this->base_url = str_replace("/" . $path, "", $url); |
131 | 137 | $this->path = $path; |
@@ -151,20 +157,21 @@ discard block |
||
151 | 157 | // so we must find the latest one(short_path) in the permutation of the main_str |
152 | 158 | $latest = collect(\DB::select("SELECT short_path FROM $tbl WHERE created_at = (SELECT MAX(created_at) FROM $tbl)")); |
153 | 159 | |
154 | - if (!$latest->count()) |
|
155 | - return $this->getFirstUrl(); |
|
160 | + if (!$latest->count()) { |
|
161 | + return $this->getFirstUrl(); |
|
162 | + } |
|
156 | 163 | |
157 | 164 | if ($latest->count() == 1) { |
158 | 165 | return $this->findNextPerm($latest->first()->short_path); |
159 | - } |
|
160 | - else { |
|
166 | + } else { |
|
161 | 167 | foreach ($latest->reverse() as $key => $item) { |
162 | 168 | $next = $this->findNextPerm($item->short_path); |
163 | 169 | |
164 | 170 | // If next permutation of current item is in fetched items |
165 | 171 | // find next permutation of the next fetched one |
166 | - if (!$latest->contains("short_path", $next)) |
|
167 | - return $next; |
|
172 | + if (!$latest->contains("short_path", $next)) { |
|
173 | + return $next; |
|
174 | + } |
|
168 | 175 | } |
169 | 176 | } |
170 | 177 | } |
@@ -10,7 +10,7 @@ discard block |
||
10 | 10 | protected $driver; |
11 | 11 | private $driver_instance; |
12 | 12 | |
13 | - public function __construct (DriverFactory $factory) |
|
13 | + public function __construct(DriverFactory $factory) |
|
14 | 14 | { |
15 | 15 | $this->factory = $factory; |
16 | 16 | } |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | * |
21 | 21 | * @return Shorturl |
22 | 22 | */ |
23 | - public function onDriver (string $driver) |
|
23 | + public function onDriver(string $driver) |
|
24 | 24 | { |
25 | 25 | $this->driver = $driver; |
26 | 26 | $this->setDriverInstance(); |
@@ -30,7 +30,7 @@ discard block |
||
30 | 30 | /** |
31 | 31 | * @return void |
32 | 32 | */ |
33 | - private function setDriverInstance () |
|
33 | + private function setDriverInstance() |
|
34 | 34 | { |
35 | 35 | $this->driver_instance = $this->factory->make($this->getDriver()); |
36 | 36 | } |
@@ -41,17 +41,17 @@ discard block |
||
41 | 41 | * |
42 | 42 | * @return string |
43 | 43 | */ |
44 | - public function getDriver (): string |
|
44 | + public function getDriver(): string |
|
45 | 45 | { |
46 | 46 | return $this->driver ?: config("shorturl.drivers.default"); |
47 | 47 | } |
48 | 48 | |
49 | - public function __call ($method, $args) |
|
49 | + public function __call($method, $args) |
|
50 | 50 | { |
51 | - return call_user_func_array(array ($this->getDriverInstance(), $method), $args); |
|
51 | + return call_user_func_array(array($this->getDriverInstance(), $method), $args); |
|
52 | 52 | } |
53 | 53 | |
54 | - private function getDriverInstance () |
|
54 | + private function getDriverInstance() |
|
55 | 55 | { |
56 | 56 | if (!$this->driver_instance) |
57 | 57 | $this->setDriverInstance(); |
@@ -10,7 +10,7 @@ |
||
10 | 10 | * |
11 | 11 | * @return LocalDriver |
12 | 12 | */ |
13 | - public function make (string $driver) |
|
13 | + public function make(string $driver) |
|
14 | 14 | { |
15 | 15 | switch ($driver) { |
16 | 16 | case "local": |