Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make arguments required for DI #264

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions src/Classes/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,21 +170,10 @@ class Builder
protected ?Closure $beforeCreateCallback = null;

/**
* Builder constructor.
*
* When constructing this class, ensure that the
* config variables are validated.
*
* @param Validation|null $validation
*
* @throws ValidationException
*/
public function __construct(UrlKeyGenerator $urlKeyGenerator, Validation $validation = null)
public function __construct(Validation $validation, UrlKeyGenerator $urlKeyGenerator)
{
if (! $validation) {
$validation = new Validation();
}

$validation->validateConfig();

$this->keyGenerator = $urlKeyGenerator;
Expand Down
19 changes: 6 additions & 13 deletions src/Classes/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,21 @@

class Resolver
{
private UserAgentDriver $userAgentDriver;

/**
* Resolver constructor.
*
* When constructing this class, ensure that the
* config variables are validated.
*
* @param Validation|null $validation
*
* @throws ValidationException
*/
public function __construct(Validation $validation = null)
public function __construct(UserAgentDriver $userAgentDriver, Validation $validation)
{
if (! $validation) {
$validation = new Validation();
}

$validation->validateConfig();

$this->userAgentDriver = $userAgentDriver;
}

/**
Expand Down Expand Up @@ -121,7 +119,7 @@ protected function recordVisit(Request $request, ShortURL $shortURL): ShortURLVi
*/
protected function trackVisit(ShortURL $shortURL, ShortURLVisit $visit, Request $request): void
{
$userAgentParser = $this->userAgentParser()->usingUserAgentString($request->userAgent());
$userAgentParser = $this->userAgentDriver->usingUserAgentString($request->userAgent());

if ($shortURL->track_ip_address) {
$visit->ip_address = $request->ip();
Expand Down Expand Up @@ -169,9 +167,4 @@ protected function guessDeviceType(UserAgentDriver $userAgentParser): ?string
default => null,
};
}

private function userAgentParser(): UserAgentDriver
{
return app(UserAgentDriver::class);
}
}
1 change: 1 addition & 0 deletions src/Providers/ShortURLProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function register(): void

$this->app->bind('short-url.builder', function (Application $app): Builder {
return new Builder(
validation: $app->make(Validation::class),
urlKeyGenerator: $app->make(UrlKeyGenerator::class),
);
});
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Classes/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use AshAllenDesign\ShortURL\Classes\Builder;
use AshAllenDesign\ShortURL\Classes\KeyGenerator;
use AshAllenDesign\ShortURL\Classes\Validation;
use AshAllenDesign\ShortURL\Exceptions\ShortURLException;
use AshAllenDesign\ShortURL\Exceptions\ValidationException;
use AshAllenDesign\ShortURL\Models\ShortURL;
Expand Down Expand Up @@ -82,7 +83,7 @@ public function destination_url_is_changed_to_https_if_enforce_https_flag_is_set
public function destination_url_is_not_changed_to_https_if_enforce_https_flag_is_set_to_false_from_the_config()
{
Config::set('short-url.enforce_https', false);
$builder = new Builder(new KeyGenerator(new Hashids()));
$builder = new Builder(new Validation(), new KeyGenerator(new Hashids()));
$shortUrl = $builder->destinationUrl('http://domain.com')->make();
$this->assertSame('http://domain.com', $shortUrl->destination_url);
}
Expand Down
26 changes: 14 additions & 12 deletions tests/Unit/Classes/ResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace AshAllenDesign\ShortURL\Tests\Unit\Classes;

use AshAllenDesign\ShortURL\Classes\Resolver;
use AshAllenDesign\ShortURL\Classes\UserAgent\ParserPhpDriver;
use AshAllenDesign\ShortURL\Classes\Validation;
use AshAllenDesign\ShortURL\Exceptions\ValidationException;
use AshAllenDesign\ShortURL\Models\ShortURL;
use AshAllenDesign\ShortURL\Models\ShortURLVisit;
Expand Down Expand Up @@ -91,7 +93,7 @@ public function exception_is_thrown_in_the_constructor_if_the_config_variables_a

Config::set('short-url.key_length', 'INVALID');

new Resolver();
app(Resolver::class);
}

/** @test */
Expand All @@ -111,7 +113,7 @@ public function request_is_aborted_if_url_is_single_use_and_has_already_been_vis

$request = Request::create(config('short-url.default_url').'/short/12345');

$resolver = new Resolver();
$resolver = app(Resolver::class);
$resolver->handleVisit($request, $shortURL);
}

Expand All @@ -129,7 +131,7 @@ public function request_is_not_aborted_if_url_is_single_use_and_has_not_been_vis

$request = Request::create(config('short-url.default_url').'/short/12345');

$resolver = new Resolver();
$resolver = app(Resolver::class);
$result = $resolver->handleVisit($request, $shortURL);

$this->assertTrue($result);
Expand All @@ -151,7 +153,7 @@ public function request_is_not_aborted_if_url_is_not_single_use_and_has_been_vis

$request = Request::create(config('short-url.default_url').'/short/12345');

$resolver = new Resolver();
$resolver = app(Resolver::class);
$result = $resolver->handleVisit($request, $shortURL);
$this->assertTrue($result);
}
Expand All @@ -170,7 +172,7 @@ public function visit_details_are_not_recorded_if_url_does_not_have_tracking_ena

$request = Request::create(config('short-url.default_url').'/short/12345');

$resolver = new Resolver();
$resolver = new Resolver(new ParserPhpDriver(), new Validation());
$result = $resolver->handleVisit($request, $shortURL);
$this->assertTrue($result);

Expand Down Expand Up @@ -215,7 +217,7 @@ public function visit_is_recorded_if_url_has_tracking_enabled(string $userAgent,
]
);

$resolver = new Resolver();
$resolver = app(Resolver::class);
$result = $resolver->handleVisit($request, $shortURL);
$this->assertTrue($result);

Expand Down Expand Up @@ -253,7 +255,7 @@ public function visit_is_recorded_if_url_has_tracking_enabled_and_the_user_agent
]
);

$resolver = new Resolver();
$resolver = app(Resolver::class);
$result = $resolver->handleVisit($request, $shortURL);
$this->assertTrue($result);

Expand Down Expand Up @@ -295,7 +297,7 @@ public function visit_is_recorded_if_url_has_tracking_enabled_and_the_user_agent
]
);

$resolver = new Resolver();
$resolver = app(Resolver::class);
$result = $resolver->handleVisit($request, $shortURL);
$this->assertTrue($result);

Expand Down Expand Up @@ -341,7 +343,7 @@ public function only_specific_fields_are_recorded_if_enabled()
]
);

$resolver = new Resolver();
$resolver = app(Resolver::class);
$result = $resolver->handleVisit($request, $shortURL);
$this->assertTrue($result);

Expand Down Expand Up @@ -372,7 +374,7 @@ public function request_is_aborted_if_url_is_single_use_and_the_tracking_is_not_

$request = Request::create(config('short-url.default_url').'/short/12345');

$resolver = new Resolver();
$resolver = app(Resolver::class);

// Visit the URL for the first time. This should be allowed.
$resolver->handleVisit($request, $shortURL);
Expand Down Expand Up @@ -407,7 +409,7 @@ public function referer_url_is_stored_if_it_is_enabled()
'HTTP_USER_AGENT' => static::trackingFieldsProvider()[1][0],
]);

$resolver = new Resolver();
$resolver = app(Resolver::class);
$result = $resolver->handleVisit($request, $shortURL);
$this->assertTrue($result);

Expand Down Expand Up @@ -443,7 +445,7 @@ public function fields_are_not_recorded_if_all_are_true_but_track_visits_is_disa
'HTTP_USER_AGENT' => static::trackingFieldsProvider()[1][0],
]);

$resolver = new Resolver();
$resolver = app(Resolver::class);
$result = $resolver->handleVisit($request, $shortURL);
$this->assertTrue($result);

Expand Down
Loading