Make skill ID optional when creating link instance

Link looks more like a service.
When dealing with dependency injection it would make sense to create a
single instance with settings.

Following calls can then ask to create a button or link with all
necessary information.
This commit is contained in:
Daniel Siepmann 2020-09-10 09:47:22 +02:00
parent ed73b6d3f0
commit bf1e666091
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
2 changed files with 22 additions and 6 deletions

View file

@ -12,7 +12,7 @@ use SkillDisplay\PHPToolKit\Configuration\Settings;
*/
class Link
{
private int $skillID;
private ?int $skillID;
private Settings $settings;
/* SVG Definitions */
@ -48,7 +48,7 @@ class Link
<circle cx="25" cy="25" r="12" stroke="white" stroke-width="2"/>
</svg>';
public function __construct(Settings $settings, int $skillID)
public function __construct(Settings $settings, ?int $skillID = null)
{
$this->settings = $settings;
$this->skillID = $skillID;
@ -58,9 +58,15 @@ class Link
* @param string $vtype one of VERIFICATION_SELF, VERIFICATION_EDUCATIONAL, VERIFICATION_BUSINESS, VERIFICATION_CERTIFICATION
* @return string URL to a Verification that a user can click, he/she will see the Verification interface and can choose a verifier
*/
public function getVerificationLink(string $vtype): string
public function getVerificationLink(string $vtype, ?int $skillID = null) : string
{
$link = $this->settings->getMySkillDisplayUrl() . '/skillup/skill/' . $this->skillID . '/0/';
$skillID = $skillID ?? $this->skillID;
if ($skillID === null) {
throw new \InvalidArgumentException('No skill ID provided.', 1599723825);
}
$link = $this->settings->getMySkillDisplayUrl() . '/skillup/skill/' . $skillID . '/0/';
switch ($vtype) {
case VERIFICATION_EDUCATIONAL:
$link .= '2';
@ -83,7 +89,7 @@ class Link
* @param string $vtype one of VERIFICATION_SELF, VERIFICATION_EDUCATIONAL, VERIFICATION_BUSINESS, VERIFICATION_CERTIFICATION
* @return string SVG Button to a Verification that a user can click, he/she will see the Verification interface and can choose a verifier
*/
public function getVerificationButton(string $vtype): string
public function getVerificationButton(string $vtype, ?int $skillID = null) : string
{
switch ($vtype) {
case VERIFICATION_EDUCATIONAL:
@ -100,7 +106,7 @@ class Link
break;
}
return <<<BUTTON
<a href="{$this->getVerificationLink($vtype)}" target="_blank">{$buttonsvg}</a>
<a href="{$this->getVerificationLink($vtype, $skillID)}" target="_blank">{$buttonsvg}</a>
BUTTON;
}
}

View file

@ -28,6 +28,16 @@ class LinkTest extends TestCase
static::assertInstanceOf(Link::class, $subject);
}
/**
* @test
*/
public function instanceCanBeCreatedWithoutSkillId()
{
$settings = $this->prophesize(Settings::class);
$subject = new Link($settings->reveal());
static::assertInstanceOf(Link::class, $subject);
}
/**
* @test
*/