Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  exposes the generated jwt so it can be used elsewhere
  library artwork size may not be known
  widen the range of accepted library ids
  artist id can be 6 to 7 chars long
  • Loading branch information
Baptouuuu committed Apr 12, 2020
2 parents 3c12def + de56ea4 commit 0104da4
Show file tree
Hide file tree
Showing 19 changed files with 91 additions and 18 deletions.
10 changes: 8 additions & 2 deletions fixtures/SDK/Library/Album/Artwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ public static function any(): Set
static function($width, $height, $url): Model {
return new Model($width, $height, $url);
},
Artwork\Width::any(),
Artwork\Height::any(),
new Set\Either(
Artwork\Width::any(),
Set\Elements::of(null),
),
new Set\Either(
Artwork\Height::any(),
Set\Elements::of(null),
),
Url::any(),
);
}
Expand Down
2 changes: 1 addition & 1 deletion fixtures/SDK/Library/Album/Id.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class Id
*/
public static function any(): Set
{
$chars = Set\Regex::for('^[a-zA-Z0-9]{7}$');
$chars = Set\Regex::for('^[a-zA-Z0-9]+$');

return Set\Decorate::immutable(
static function(string $chars): Model {
Expand Down
2 changes: 1 addition & 1 deletion fixtures/SDK/Library/Artist/Id.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class Id
*/
public static function any(): Set
{
$chars = Set\Regex::for('^[a-zA-Z0-9]{7}$');
$chars = Set\Regex::for('^[a-zA-Z0-9]+$');

return Set\Decorate::immutable(
static function(string $chars): Model {
Expand Down
2 changes: 1 addition & 1 deletion fixtures/SDK/Library/Song/Id.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class Id
*/
public static function any(): Set
{
$chars = Set\Regex::for('^[a-zA-Z0-9]{14}$');
$chars = Set\Regex::for('^[a-zA-Z0-9]+$');

return Set\Decorate::immutable(
static function(string $chars): Model {
Expand Down
1 change: 1 addition & 0 deletions src/SDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

interface SDK
{
public function jwt(): string;
public function storefronts(): SDK\Storefronts;
public function library(string $userToken): SDK\Library;
public function catalog(SDK\Storefront\Id $storefront): SDK\Catalog;
Expand Down
22 changes: 18 additions & 4 deletions src/SDK/Library/Album/Artwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,41 @@

final class Artwork
{
private Width $width;
private Height $height;
private ?Width $width;
private ?Height $height;
private Url $url;

public function __construct(
Width $width,
Height $height,
?Width $width,
?Height $height,
Url $url
) {
$this->width = $width;
$this->height = $height;
$this->url = $url;
}

public function widthKnown(): bool
{
return $this->width instanceof Width;
}

/** @psalm-suppress InvalidNullableReturnType */
public function width(): Width
{
/** @psalm-suppress NullableReturnStatement */
return $this->width;
}

public function heightKnown(): bool
{
return $this->height instanceof Height;
}

/** @psalm-suppress InvalidNullableReturnType */
public function height(): Height
{
/** @psalm-suppress NullableReturnStatement */
return $this->height;
}

Expand Down
9 changes: 9 additions & 0 deletions src/SDK/Library/Album/Artwork/Height.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ public function __construct(int $value)
$this->value = $value;
}

public static function of(?int $value): ?self
{
if (\is_null($value)) {
return null;
}

return new self($value);
}

public function toInt(): int
{
return $this->value;
Expand Down
9 changes: 9 additions & 0 deletions src/SDK/Library/Album/Artwork/Width.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ public function __construct(int $value)
$this->value = $value;
}

public static function of(?int $value): ?self
{
if (\is_null($value)) {
return null;
}

return new self($value);
}

public function toInt(): int
{
return $this->value;
Expand Down
2 changes: 1 addition & 1 deletion src/SDK/Library/Album/Id.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class Id

public function __construct(string $value)
{
if (!Str::of($value)->matches('~^l\.[a-zA-Z0-9]{7}$~')) {
if (!Str::of($value)->matches('~^l\.[a-zA-Z0-9]+$~')) {
throw new DomainException($value);
}

Expand Down
2 changes: 1 addition & 1 deletion src/SDK/Library/Artist/Id.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class Id

public function __construct(string $value)
{
if (!Str::of($value)->matches('~^r\.[a-zA-Z0-9]{7}$~')) {
if (!Str::of($value)->matches('~^r\.[a-zA-Z0-9]+$~')) {
throw new DomainException($value);
}

Expand Down
4 changes: 2 additions & 2 deletions src/SDK/Library/Library.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ public function albums(Artist\Id $artist): Set
new Album\Id($album['id']),
new Album\Name($album['attributes']['name']),
\array_key_exists('artwork', $album['attributes']) ? new Album\Artwork(
new Album\Artwork\Width($album['attributes']['artwork']['width']),
new Album\Artwork\Height($album['attributes']['artwork']['height']),
Album\Artwork\Width::of($album['attributes']['artwork']['width']),
Album\Artwork\Height::of($album['attributes']['artwork']['height']),
Url::of($album['attributes']['artwork']['url']),
) : null,
...\array_map(
Expand Down
2 changes: 1 addition & 1 deletion src/SDK/Library/Song/Id.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class Id

public function __construct(string $value)
{
if (!Str::of($value)->matches('~^i\.[a-zA-Z0-9]{14}$~')) {
if (!Str::of($value)->matches('~^i\.[a-zA-Z0-9]+$~')) {
throw new DomainException($value);
}

Expand Down
9 changes: 8 additions & 1 deletion src/SDK/SDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ final class SDK implements SDKInterface
private Transport $transport;
private Clock $clock;
private Header $authorization;
private string $jwt;

public function __construct(
Clock $clock,
Expand All @@ -48,12 +49,18 @@ public function __construct(

$this->clock = $clock;
$this->transport = new HttpTransport\AppleMusic($transport);
$this->jwt = (string) $jwt;
$this->authorization = new Header(
'Authorization',
new Value('Bearer '.(string) $jwt),
new Value('Bearer '.$this->jwt),
);
}

public function jwt(): string
{
return $this->jwt;
}

public function storefronts(): Storefronts
{
return new Storefronts\Storefronts($this->transport, $this->authorization);
Expand Down
13 changes: 13 additions & 0 deletions tests/SDK/Library/Album/Artwork/HeightTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
SDK\Library\Album\Artwork\Height,
Exception\DomainException,
};
use Fixtures\MusicCompanion\AppleMusic\SDK\Library\Album\Artwork\Height as HeightSet;
use PHPUnit\Framework\TestCase;
use Innmind\BlackBox\{
PHPUnit\BlackBox,
Expand All @@ -29,6 +30,18 @@ public function testItCanBeOfAnyNaturalNumber()
});
}

public function testOf()
{
$this
->forAll(HeightSet::any())
->then(function($height) {
$this->assertInstanceOf(Height::class, Height::of($height->toInt()));
$this->assertSame($height->toInt(), Height::of($height->toInt())->toInt());
});

$this->assertNull(Height::of(null));
}

public function testNegativeNumbersAreNotAccepted()
{
$this
Expand Down
13 changes: 13 additions & 0 deletions tests/SDK/Library/Album/Artwork/WidthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
SDK\Library\Album\Artwork\Width,
Exception\DomainException,
};
use Fixtures\MusicCompanion\AppleMusic\SDK\Library\Album\Artwork\Width as WidthSet;
use PHPUnit\Framework\TestCase;
use Innmind\BlackBox\{
PHPUnit\BlackBox,
Expand All @@ -29,6 +30,18 @@ public function testItCanBeOfAnyNaturalNumber()
});
}

public function testOf()
{
$this
->forAll(WidthSet::any())
->then(function($width) {
$this->assertInstanceOf(Width::class, Width::of($width->toInt()));
$this->assertSame($width->toInt(), Width::of($width->toInt())->toInt());
});

$this->assertNull(Width::of(null));
}

public function testNegativeNumbersAreNotAccepted()
{
$this
Expand Down
2 changes: 1 addition & 1 deletion tests/SDK/Library/Album/IdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class IdTest extends TestCase

public function testStringsOfSpecifiedFormatAreAccepted()
{
$chars = Set\Regex::for('^[a-zA-Z0-9]{7}$');
$chars = Set\Regex::for('^[a-zA-Z0-9]+$');

$this
->forAll($chars)
Expand Down
2 changes: 1 addition & 1 deletion tests/SDK/Library/Artist/IdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class IdTest extends TestCase

public function testStringsOfSpecifiedFormatAreAccepted()
{
$chars = Set\Regex::for('^[a-zA-Z0-9]{7}$');
$chars = Set\Regex::for('^[a-zA-Z0-9]+$');

$this
->forAll($chars)
Expand Down
2 changes: 1 addition & 1 deletion tests/SDK/Library/Song/IdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class IdTest extends TestCase

public function testStringsOfSpecifiedFormatAreAccepted()
{
$chars = Set\Regex::for('^[a-zA-Z0-9]{14}$');
$chars = Set\Regex::for('^[a-zA-Z0-9]+$');

$this
->forAll($chars)
Expand Down
1 change: 1 addition & 0 deletions tests/SDK/SDKTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public function testInterface()
$this->assertInstanceOf(Storefronts::class, $sdk->storefronts());
$this->assertInstanceOf(Catalog::class, $sdk->catalog($storefront));
$this->assertInstanceOf(Library::class, $sdk->library($userToken));
$this->assertNotEmpty($sdk->jwt());
$sdk->storefronts()->all(); // trigger the assertion on the transport
});
}
Expand Down

0 comments on commit 0104da4

Please sign in to comment.