Skip to content

Commit 564fa31

Browse files
authored
Merge pull request #3 from code16/v2
V2
2 parents c8541b1 + dfe5838 commit 564fa31

13 files changed

+144
-73
lines changed

src/Api/Clients/TiteLive/TiteLiveApiException.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44

55
use Exception;
66

7-
class TiteLiveApiException extends Exception
8-
{
9-
}
7+
class TiteLiveApiException extends Exception {}

src/Api/SuggestBooksFromAuthors.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class SuggestBooksFromAuthors extends CacheableAction
1212

1313
protected ?string $excludedGencod;
1414

15-
public function suggestBooks(array $authors, string $excludedGencod = null): Collection
15+
public function suggestBooks(array $authors, ?string $excludedGencod = null): Collection
1616
{
1717
$this->authors = $authors;
1818
$this->excludedGencod = $excludedGencod;

src/Api/SuggestOtherEditions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class SuggestOtherEditions extends CacheableAction
1414

1515
protected ?string $excludedGencod;
1616

17-
public function suggestBooks(string $bookTitle, array $authors, string $excludedGencod = null): Collection
17+
public function suggestBooks(string $bookTitle, array $authors, ?string $excludedGencod = null): Collection
1818
{
1919
$this->bookTitle = $bookTitle;
2020
$this->authors = $authors;

src/Book.php

Lines changed: 24 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Code16\LaravelTiteliveClient;
44

55
use Code16\LaravelTiteliveClient\Database\Factories\BookFactory;
6+
use Code16\LaravelTiteliveClient\Enum\BookAvailability;
7+
use Illuminate\Database\Eloquent\Casts\Attribute;
68
use Illuminate\Database\Eloquent\Factories\HasFactory;
79
use Illuminate\Database\Eloquent\Model;
810
use Illuminate\Support\Str;
@@ -12,71 +14,39 @@ class Book extends Model implements JsonSerializable
1214
{
1315
use HasFactory;
1416

15-
public static int $AVAILABLE_ON_DEMAND = 1; // Sur commande
16-
17-
public static int $FORTHCOMING = 2; // À paraître
18-
19-
public static int $REPRINT = 3; // En réimpression
20-
21-
public static int $UNAVAILABLE = 4; // Indisponible
22-
23-
public static int $COMMERCIAL_CHANGE = 5; // Changement de distributeur
24-
25-
public static int $OUT_OF_PRINT = 6; // Épuisé
26-
27-
public static int $MISSING = 7; // Manque sans date
28-
29-
public static int $TO_BE_PUBLISHED_AGAIN = 8; // À reparaître
30-
3117
protected $guarded = [];
3218

3319
protected $casts = [
34-
'published_date' => 'date',
3520
'id' => 'string',
21+
'published_date' => 'date',
22+
'availability' => BookAvailability::class,
3623
];
3724

3825
protected static function newFactory()
3926
{
40-
return new BookFactory();
41-
}
42-
43-
public function getUrlAttribute(): string
44-
{
45-
return route('book.show', [
46-
'id' => $this->id,
47-
'slug' => Str::slug($this->title),
48-
]);
27+
return new BookFactory;
4928
}
5029

51-
public function getAvailabilityLabelAttribute(): ?string
30+
protected function url(): Attribute
5231
{
53-
if ($this->hasStock() && $this->availability != self::$FORTHCOMING) {
54-
return null;
55-
}
56-
57-
switch ($this->availability) {
58-
case self::$AVAILABLE_ON_DEMAND:
59-
return 'Sur commande';
60-
case self::$FORTHCOMING:
61-
return 'À paraître';
62-
case self::$OUT_OF_PRINT:
63-
return 'Épuisé';
64-
case self::$REPRINT:
65-
case self::$TO_BE_PUBLISHED_AGAIN:
66-
return 'À reparaître';
67-
default:
68-
return 'Indisponible à la vente';
69-
}
32+
return Attribute::make(
33+
get: fn () => route('book.show', [
34+
'id' => $this->id,
35+
'slug' => Str::slug($this->title),
36+
])
37+
);
7038
}
7139

72-
public function getShortDetailsAttribute(): string
40+
protected function shortDetails(): Attribute
7341
{
74-
return collect([
75-
collect($this->authors)->join(', '),
76-
$this->editor,
77-
])
78-
->filter()
79-
->join('');
42+
return Attribute::make(
43+
get: fn () => collect([
44+
collect($this->authors)->join(', '),
45+
$this->editor,
46+
])
47+
->filter()
48+
->join('')
49+
);
8050
}
8151

8252
public function hasStock(): bool
@@ -86,12 +56,12 @@ public function hasStock(): bool
8656

8757
public function canBeOrdered(): bool
8858
{
89-
if (config('maktaba.shopping_closed')) {
59+
if (config('titelive-client.shopping_closed')) {
9060
return false;
9161
}
9262

93-
return $this->availability != self::$FORTHCOMING
94-
&& ($this->hasStock() || $this->availability == self::$AVAILABLE_ON_DEMAND);
63+
return $this->availability != BookAvailability::Forthcoming
64+
&& ($this->hasStock() || $this->availability == BookAvailability::AvailableOnDemand);
9565
}
9666

9767
public function visual(string $size): string

src/Enum/BookAvailability.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Code16\LaravelTiteliveClient\Enum;
4+
5+
enum BookAvailability: int
6+
{
7+
case AvailableOnDemand = 1;
8+
case Forthcoming = 2;
9+
case Reprint = 3;
10+
case Unavailable = 4;
11+
case CommercialChange = 5;
12+
case OutOfPrint = 6;
13+
case Missing = 7;
14+
case ToBePublishedAgain = 8;
15+
16+
public function getLabel(): string
17+
{
18+
return match ($this) {
19+
BookAvailability::AvailableOnDemand => 'Sur commande',
20+
BookAvailability::Forthcoming => 'À paraître',
21+
BookAvailability::Reprint,
22+
BookAvailability::ToBePublishedAgain => 'À reparaître',
23+
BookAvailability::Unavailable,
24+
BookAvailability::Missing,
25+
BookAvailability::CommercialChange => 'Indisponible',
26+
BookAvailability::OutOfPrint => 'Épuisé',
27+
};
28+
}
29+
}

src/LaravelTiteliveClientServiceProvider.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@ public function packageRegistered()
2424
{
2525
$this->app->bind(BookDirectoryClient::class, function () {
2626
if (config('titelive-client.book_directory.mock', false)) {
27-
return new BookDirectoryMockClientForDev();
27+
return new BookDirectoryMockClientForDev;
2828
}
2929

3030
return new TiteLiveClient(
31-
config('services.titelive.endpoint'),
32-
config('services.titelive.client_number'),
33-
config('services.titelive.login'),
34-
config('services.titelive.password'),
31+
config('titelive-client.api.endpoint'),
32+
config('titelive-client.api.client_number'),
33+
config('titelive-client.api.login'),
34+
config('titelive-client.api.password'),
3535
);
3636
});
3737

3838
$this->app->bind(BookCache::class, function ($app) {
3939
if (config('titelive-client.book_directory.mock', false)) {
40-
return new BookCacheMockForDev();
40+
return new BookCacheMockForDev;
4141
}
4242

4343
return $app->get(RandomBasedOnRefreshDateBookCache::class);

src/Utils/HasBookAttribute.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Code16\LaravelTiteliveClient\Utils;
4+
5+
use Code16\LaravelTiteliveClient\Api\Clients\BookCache;
6+
use Code16\LaravelTiteliveClient\Book;
7+
use Illuminate\Database\Eloquent\Casts\Attribute;
8+
9+
/**
10+
* @mixin \Illuminate\Database\Eloquent\Model
11+
*/
12+
trait HasBookAttribute
13+
{
14+
public function refreshBook(bool $force = false): self
15+
{
16+
$this->update([
17+
'book' => app(BookCache::class)
18+
->force($force)
19+
->refreshIfNeeded($this->book),
20+
]);
21+
22+
return $this;
23+
}
24+
25+
public function book(): Attribute
26+
{
27+
return Attribute::make(
28+
get: fn () => $this->attributes['book'] ?? null
29+
? new Book($this->fromJson($this->attributes['book']))
30+
: null
31+
);
32+
}
33+
}

src/Utils/HasBooksAttribute.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Code16\LaravelTiteliveClient\Utils;
4+
5+
use Code16\LaravelTiteliveClient\Api\Clients\BookCache;
6+
use Code16\LaravelTiteliveClient\Book;
7+
use Illuminate\Database\Eloquent\Casts\Attribute;
8+
use Illuminate\Support\Collection;
9+
10+
/**
11+
* @property Collection|Book[] $books
12+
*
13+
* @mixin \Illuminate\Database\Eloquent\Model
14+
*/
15+
trait HasBooksAttribute
16+
{
17+
public function refreshBooks(bool $force = false): self
18+
{
19+
$this->update([
20+
'books' => $this->books
21+
->map(function (Book $book) use ($force) {
22+
return app(BookCache::class)
23+
->force($force)
24+
->refreshIfNeeded($book);
25+
})
26+
->filter()
27+
->values()
28+
->toArray(),
29+
]);
30+
31+
return $this;
32+
}
33+
34+
public function books(): Attribute
35+
{
36+
return Attribute::make(
37+
get: fn () => collect($this->fromJson($this->attributes['book'] ?? []))
38+
->whereNotNull()
39+
->map(fn ($attributes) => new Book($attributes))
40+
->values()
41+
);
42+
}
43+
}

tests/Api/FindBookTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Code16\LaravelTiteliveClient\Api\FindBook;
66

77
beforeEach(function () {
8-
$this->fakeClient = new BookDirectoryMockClientForDev();
8+
$this->fakeClient = new BookDirectoryMockClientForDev;
99
$this->app->bind(BookDirectoryClient::class, fn () => $this->fakeClient);
1010
});
1111

tests/Api/ListBooksTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Code16\LaravelTiteliveClient\Api\ListBooks;
66

77
beforeEach(function () {
8-
$this->fakeClient = new BookDirectoryMockClientForDev();
8+
$this->fakeClient = new BookDirectoryMockClientForDev;
99
$this->app->bind(BookDirectoryClient::class, fn () => $this->fakeClient);
1010
});
1111

tests/Api/SearchBooksTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Code16\LaravelTiteliveClient\Api\SearchBooks;
66

77
beforeEach(function () {
8-
$this->fakeClient = new BookDirectoryMockClientForDev();
8+
$this->fakeClient = new BookDirectoryMockClientForDev;
99
$this->app->bind(BookDirectoryClient::class, fn () => $this->fakeClient);
1010
});
1111

tests/Api/SuggestBooksFromAuthorsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Illuminate\Support\Collection;
88

99
it('suggests books from authors', function () {
10-
$fakeClient = new BookDirectoryMockClientForDev();
10+
$fakeClient = new BookDirectoryMockClientForDev;
1111
$this->app->bind(BookDirectoryClient::class, fn () => $fakeClient);
1212

1313
app(SuggestBooksFromAuthors::class)->suggestBooks(['bob marley', 'doc gyneco']);

tests/Api/SuggestOtherEditionsTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
use Code16\LaravelTiteliveClient\Book;
77
use Illuminate\Support\Collection;
88

9-
beforeEach(function () {
10-
11-
});
9+
beforeEach(function () {});
1210

1311
it('suggests other editions of a book', function () {
14-
$fakeClient = new BookDirectoryMockClientForDev();
12+
$fakeClient = new BookDirectoryMockClientForDev;
1513
$this->app->bind(BookDirectoryClient::class, fn () => $fakeClient);
1614

1715
app(SuggestOtherEditions::class)->suggestBooks('How to make $$', ['bob marley', 'doc gyneco']);

0 commit comments

Comments
 (0)