33namespace CodeOfDigital \LaravelUrlShortener ;
44
55use CodeOfDigital \LaravelUrlShortener \Contracts \UrlFactory ;
6+ use CodeOfDigital \LaravelUrlShortener \Drivers \BitLyDriverShortener ;
7+ use GuzzleHttp \ClientInterface ;
68use http \Exception \InvalidArgumentException ;
79use Illuminate \Foundation \Application ;
10+ use Illuminate \Support \Arr ;
811use Illuminate \Support \Str ;
912
10- class UrlShortenerInstance implements UrlFactory
13+ class UrlShortener implements UrlFactory
1114{
1215 protected $ app ;
1316 protected $ shorteners ;
@@ -23,6 +26,33 @@ public function __construct(Application $app)
2326 $ this ->shorteners = [];
2427 }
2528
29+ /**
30+ * Dynamically call the default driver instance
31+ *
32+ * @param $method
33+ * @param $parameters
34+ * @return mixed
35+ */
36+ public function __call ($ method , $ parameters )
37+ {
38+ return $ this ->driver ()->$ method (...$ parameters );
39+ }
40+
41+ /**
42+ * Create an instance of Bit.ly driver
43+ *
44+ * @param array $config
45+ * @return BitLyDriverShortener
46+ */
47+ protected function createBitLyDriver (array $ config )
48+ {
49+ return new BitLyDriverShortener (
50+ $ this ->app ->make (ClientInterface::class),
51+ Arr::get ($ config , 'token ' ),
52+ Arr::get ($ config , 'domain ' , 'bit.ly ' )
53+ );
54+ }
55+
2656 /**
2757 * Get the default URL shortener driver
2858 *
@@ -38,18 +68,41 @@ public function getUrlDefaultDriver(): string
3868 *
3969 * @return $this
4070 */
41- public function setUrlDefaultDriver ($ name ): UrlShortenerInstance
71+ public function setUrlDefaultDriver ($ name ): UrlShortener
4272 {
4373 $ this ->app ['config ' ]['url-shortener.default ' ] = $ name ;
4474 return $ this ;
4575 }
4676
47- public function getUrlShortenerConfig ($ name )
77+ /**
78+ * Get the URL shortener configuration in array form
79+ *
80+ * @param $name
81+ * @return mixed
82+ */
83+ protected function getUrlShortenerConfig ($ name )
84+ {
85+ return $ this ->app ['config ' ]["url-shortener.shorteners. {$ name }" ];
86+ }
87+
88+ /**
89+ * Get a URL shortener driver instance
90+ *
91+ * @param null $name
92+ * @return mixed
93+ */
94+ public function driver ($ name = null )
4895 {
49- return config ( " url- shortener.shorteners. { $ name}" );
96+ return $ this -> shortener ( $ name );
5097 }
5198
52- public function resolveUrlDriver ($ name )
99+ /**
100+ * Resolve the URL driver by creating an instance of the given URL shortener
101+ *
102+ * @param $name
103+ * @return mixed
104+ */
105+ protected function resolveUrlDriver ($ name )
53106 {
54107 $ config = $ this ->getUrlShortenerConfig ($ name );
55108
0 commit comments