Skip to content

Commit 8e7ce3a

Browse files
committed
v2
1 parent c8541b1 commit 8e7ce3a

File tree

5 files changed

+131
-57
lines changed

5 files changed

+131
-57
lines changed

src/Book.php

Lines changed: 23 additions & 53 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
{
4027
return new BookFactory();
4128
}
4229

43-
public function getUrlAttribute(): string
44-
{
45-
return route('book.show', [
46-
'id' => $this->id,
47-
'slug' => Str::slug($this->title),
48-
]);
49-
}
50-
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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ public function packageRegistered()
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

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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
* @mixin \Illuminate\Database\Eloquent\Model
13+
*/
14+
trait HasBooksAttribute
15+
{
16+
public function refreshBooks(bool $force = false): self
17+
{
18+
$this->update([
19+
'books' => $this->books
20+
->map(function(Book $book) use($force) {
21+
return app(BookCache::class)
22+
->force($force)
23+
->refreshIfNeeded($book);
24+
})
25+
->filter()
26+
->values()
27+
->toArray()
28+
]);
29+
30+
return $this;
31+
}
32+
33+
public function books(): Attribute
34+
{
35+
return Attribute::make(
36+
get: fn () => collect($this->fromJson($this->attributes['book'] ?? []))
37+
->whereNotNull()
38+
->map(fn ($attributes) => new Book($attributes))
39+
->values()
40+
);
41+
}
42+
}

0 commit comments

Comments
 (0)