Slide 1

Slide 1 text

-BSBWFMΛ࢝Ίͯ %%%Λ࣮ફ͢Δ·Ͱ  Laravel Meetup Tokyo Vol.10 େڮ Ұथ @kzkohashi

Slide 2

Slide 2 text

େڮҰथ!L[LPIBTIJ w ΠϯϑϧΤϯαʔϚʔέςΟϯά w αʔόαΠυ ϑϩϯτத৺ w -BSBWFM࢝Ίͯ̍೥͘Β͍ w %%%͸ษڧ࢝͠Ίͯ਺ϲ݄

Slide 3

Slide 3 text

%%%஌ͬͯΔ PS %%%࣮ફͯ͠Δํ

Slide 4

Slide 4 text

ࠓ೔͓࿩͢͠Δ͜ͱ -BSBWFMͰ%%%Λ࣮ફ͢Δ·Ͱͷաఔ %%%Λ࣮ફ͢Δ͜ͱͰԿ͕มΘͬͨʁ ٯʹฉ͍ͯΈ͍ͨ

Slide 5

Slide 5 text

 -BSBWFM࢝ΊΔ

Slide 6

Slide 6 text

͍͍ɻΊͪΌ͍͍ɻ w ͙͢ʹؾ࣋ͪΑ͔͚͘Δ w ཉ͍͠ػೳ͕σϑΥϧτͰ͋Δʢ֦ு͠΍͍͢ʂʂ  w ϥΠϒϥϦ๛෋ w ೔ຊޠॆ࣮ ॻ͖΍͍͢෼ɺઃܭΛ͔ͬ͠Γ͠ͱ͖͍ͨ

Slide 7

Slide 7 text

%%%Λษڧ࢝͠ΊΔ

Slide 8

Slide 8 text

͍͍ɻΊͪΌ͍͍ɻ w ݱ࣮ͷؔ৺ΛͰ͖Δ͚ͩ։ൃ্ͱͰγϯΫϩͤ͞Δ w υϝΠϯΤΩεύʔτͱΤϯδχΞͷҰॹʹ࿩͠߹ͬͯϞ σϦϯά͍ͯ͘͠ w ڞ௨ݴޠʢϢϏΩλεݴޠʣͷൃ۷ w ͳΜͲ΋มߋɾվળ͍ͯ͘͠΋ͷ

Slide 9

Slide 9 text

֓೦͸ૉ੖Β͍͠ʂ

Slide 10

Slide 10 text

Ͱɺ࣮ફํ๏͸ʁ

Slide 11

Slide 11 text

࣮ફͷෑډ͸ߴ͍ʁ w ຊಡΉ͚ͩ͡ΌΑ͘Θ͔Βͳ͍ w ࣮ફͯ͠ͳ͕Β΍ͬͯͨ΄͏͕Αͦ͞͏ w 8FCʹ͸࣮ફ͢ΔͨΊͷ৭ʑͳΞʔΩςΫνϟͳͲ͕͋Δ ௐ΂ͨͱ͖ʹΑ͘࠾༻͞Ε͍ͯΔ 3FQPTJUPSZύλʔϯΛಋೖ

Slide 12

Slide 12 text

$POUSPMMFS 4FSWJDF 3FQPTJUPSZ &MPRVFOU3FQPTJUPSZ .PEFM ϓϨθϯςʔγϣϯ ϏδωεϩδοΫ σʔλૢ࡞ ϏδωεϩδοΫ͔Βσʔλૢ࡞ͷॲཧΛ੾Γ཭͢
 ʢ˞ඞͣ͠΋ͦ͏Ͱͳ͍ʣ

Slide 13

Slide 13 text

ͱ͋ΔαʔϏεͷྫ Ϣʔεέʔε w ϝʔΧʔ͸ΞΧ΢ϯτΛొ࿥͢Δ w ϝʔΧʔ͸ΞΧ΢ϯτΛෳ਺࡞ΕΔ Ϟσϧ

Slide 14

Slide 14 text

interface MakerAccountRepository { public function getLoginAccountByEmail($email); public function findById($id); public function register($name, $tel, $email, $password); } 3FQPTJUPSZ ར༻͢ΔࡍͷΠϯλʔϑΣʔε

Slide 15

Slide 15 text

class EloquentMakerAccountRepository implements MakerAccountRepository { /** @var \App\Models\MakerAccount */ protected $maker_account; public function __construct(MakerAccount $maker_account) { $this->maker_account = $maker_account; } public function getLoginUserByEmail($email) { return $this->maker_account->whereEmail($email) ->with('maker') ->first(); } public function register($maker_id, …) { $data[‘…’] = $maker_id; … return $this->maker_account->create($data); } } &MPRVFOU3FQPTJUPSZ .PEFMΛ%*

Slide 16

Slide 16 text

class MakerAccountRegisterService { protected $maker_repo; protected $maker_account_repo; public function __construct( MakerRepository $maker_repo, MakerAccountRepository $maker_account_repo ) { $this->maker_repo = $maker_repo; $this->maker_account_repo = $maker_account_repo; } protected function register($maker_name,…লུ) { return \DB::transaction(function () use (...লུ) { /** @var \App\Models\Maker $maker */ $maker = $this->maker_repo->register($maker_name, $address); $this->maker_account_repo->register( $maker->id, $staff_name, $tel, $email, $password ); }); } } 4FSWJDF ϦϙδτϦʔΛ௨ͯ͠ొ࿥

Slide 17

Slide 17 text

Կ͔มΘͬͨʁ w ϏδωεϩδοΫͱσʔλૢ࡞Λେମ੾Γ཭ͤͨ w ݁ہฦ͍ͯ͠Δͷ͕&MPRVFOUͷͨΊɺ֎ଆͰૢ࡞Մೳ σʔλૢ࡞͕౷Ұ͞ΕͯΔ෩ͳ΋ͷ͚͕ͩͰ͖ͨ

Slide 18

Slide 18 text

େ͖ͳצҧ͍ %%%͸ઃܭͱ͍͏ͷΛ๨Εͣʹ

Slide 19

Slide 19 text

ΤϯδχΞ૿͑ͨ ਓਓ ˞ϝΠϯ࣮૷ऀͷ͕Ͳ͔ͬʹ͍·͢ !BJJSP

Slide 20

Slide 20 text

ࠓ౓࣮ͦ͜ફ w Կ͔Β࢝ΊͨΒ͍͍ʁ w όϦϡʔΦϒδΣΫτ͔Β࢝ΊͨΒ΍Γ΍͍͢ͱຢฉ͖ !BJJSP

Slide 21

Slide 21 text

όϦϡʔΦϒδΣΫτͱ͸ʁ w ϓϦϛςΟϒܕͰදݱ͍ͯͨ͠஋ΛΦϒδΣΫτʹ͢Δ w ࣗ਎ͷՃ޻ɾ൑ఆͳͲͷৼΔ෣͍Λ࣋ͭ w ΠϛϡʔλϒϧͰ͋Δ

Slide 22

Slide 22 text

$POUSPMMFS 4FSWJDF 3FQPTJUPSZ &MPRVFOU3FQPTJUPSZ .PEFM ෦෼తʹ7BMVF0CKFDUʹม׵ υϝΠϯ૚ʁ 7BMVF0CKFDU

Slide 23

Slide 23 text

ͱ͋ΔαʔϏεͷྫ Ϣʔεέʔε w ϝʔΧʔ͸ΞΧ΢ϯτΛొ࿥͢Δ w ొ࿥͞Ε͍ͯΔϝʔϧ͕ϒϥοΫϦετͷ৔߹YYY͢Δ Ϟσϧ &NBJMΛ7BMVF0CKFDUʹͯ͠ΈΔ

Slide 24

Slide 24 text

class Email implements \JsonSerializable { protected $value; protected function __construct(string $value) { $this->value = $value; } public function value(): string { return $this->value; } public function isBlackList(): boolean { return ($this->value === $blacklist) } public function jsonSerialize() { return $this->value; } } 7BMVF0CKFDU ৼΔ෣͍Λ࣋ͨͤΔ

Slide 25

Slide 25 text

class MakerAccount extends Model { protected $table = 'maker_accounts'; protected $hidden = [ 'id', ]; public function toValueObject() { // Πϛϡʔλϒϧʹ͢ΔͨΊɺreplicate()ͨ݁͠ՌΛฦ٫ $replication = $this->replicate(); $replication->email = new Email($this->email); return $replication; } } .PEFM ର৅ͷ7BMVF0CKFDUΛ٧ΊࠐΉ

Slide 26

Slide 26 text

class MakerAccountRegisterService { protected $maker_account_repo; public function __construct(MakerAccountRepository $maker_account_repo) { $this->maker_account_repo = $maker_account_repo; } public function activateEmail($email) { $maker_account = $this->maker_account_repo->findByEmail($email); $maker_account = $maker_account->toValueObject(); return $maker_account->email->isBlackList(); } } 4FSWJDF 7BMVF0CKFDUͷৼΔ෣͍Λར༻

Slide 27

Slide 27 text

ײ૝ w 7BMVF0CKFDUͷΈͩͱखܰʹ࢝ΊΒΕΔ w ৼΔ෣͍Λू໿͢Δ͜ͱͰΘ͔Γ΍͘͢ͳͬͨ গͣͭ͠%%%ͷԸܙ͕ड͚ΒΕ͖ͯͨɾɾʁ

Slide 28

Slide 28 text

 ৽نαʔϏεͷ։ൃ

Slide 29

Slide 29 text

ϑϧͰ%%%ͷ࣮ફ νϟϯεʂ IUUQTHJUIVCDPNTIJOYMBSBWFMEEETBNQMF ৽ݪ͞ΜͷΊͪΌͪ͘Όࢀߟʹͯ͠·͢

Slide 30

Slide 30 text

ΤϯςΟςΟͱ͸ w ҰҙʹࣝผՄೳͳΦϒδΣΫτ w ෳ਺ͷ஋ΦϒδΣΫτ΍ৼΔ෣͍Λ͍࣋ͬͯΔ w ৼΔ෣͍͕গͳ͍৔߹͸ݕ౼͢΂͖

Slide 31

Slide 31 text

$POUSPMMFS 6TF$BTF 3FQPTJUPSZ &MPRVFOU3FQPTJUPSZ .PEFM υϝΠϯ૚ &OUJUZΛฦ͢ 7BMVF0CKFDU &OUJUZ

Slide 32

Slide 32 text

ͱ͋ΔαʔϏεͷྫ Ϣʔεέʔε w ը໘͔ΒϝʔΧʔ͕ ࣗ਎ͷ ΞΧ΢ϯτҰཡӾཡ͢Δ w ϝʔΧʔͷΞΧ΢ϯτͷҰཡΛϖʔδωʔγϣϯͰฦ͢ Ϟσϧ ϞσϧΛ&OUJUZʹͯ͠ΈΔ

Slide 33

Slide 33 text

class MakerAccount implements \Arrayable { /** @var MakerId */ protected $maker_id; protected $name; protected $tel; protected $email; protected $password; public function __construct(MakerId $maker_id,…লུ) { $this->maker_id = $maker_id; $this->email = $email; if ($password) { $this->password = MakerAccountPassword::of(\Hash::make($password->value())); } } public function toArray() { $array = []; $class_vars = array_keys(get_class_vars(static::class)); foreach ($class_vars as $var_name) { $array[$var_name] = $this->{$var_name}; } return $array; } } &OUJUZ ֤7BMVF0CKFDUΛ٧ΊࠐΉ

Slide 34

Slide 34 text

class MakerAccount extends Model implements Domainable { public function maker() { return $this->belongsTo(Maker::class); } /** * @return \App\Domains\Entities\MakerAccount */ public function toDomain() { return new Entities\MakerAccount( MakerId::of($this->maker_id), MakerAccountName::of($this->name), MakerAccountTel::of($this->tel), MakerAccountEmail::of($this->email) ); } } .PEFM &OUJUZʹม׵

Slide 35

Slide 35 text

class DomainServiceProvider extends ServiceProvider { … protected function collectionMacro() { Builder::macro('toDomainCollection', function () { /** @noinspection PhpUndefinedMethodInspection */ return $this->get()->map(function (Domainable $model) { return $model->toDomain(); }); }); Collection::macro('toDomainCollection', function () { /** @noinspection PhpUndefinedMethodInspection */ return $this->map(function (Domainable $model) { return $model->toDomain(); }); }); } protected function paginateMacro() { … } } $PMMFDUJPO #VJMEFS͔Β%PNBJOͷ഑ྻΛฦ͢.BDSPΛ௥Ճ

Slide 36

Slide 36 text

class FetchMakerAccountList { protected $maker_account_repo; public function __construct(MakerAccountRepository $maker_account_repo) { $this->maker_account_repo = $maker_account_repo; } protected function common(MakerAccountId $maker_account_id) { return $this->maker_account_repo->findByMakerIdAsModel($maker_id); } public function asCollection(MakerId $maker_id) { return $this->common($maker_id)->toDomainCollection(); } public function asPaginate(MakerId $maker_id) { return $this->common($maker_id)->domainPaginate(); } } 6TF$BTF ೋͭͷৼΔ෣͍Λ࣋ͨͤͯͨ

Slide 37

Slide 37 text

class MakerAccountController extends Controller { public function index(FetchMakerAccountList $usecase) { $maker_accounts = $usecase->asPaginate(MakerId::of(auth()->maker->id())); return response()->json($maker_accounts); } } $POUSPMMFS ར༻

Slide 38

Slide 38 text

6TF$BTFʹෳ਺ͷৼΔ෣͍΋ͨͤͯ͠·͍ͬͯΔ 6TF$BTFͷमਖ਼

Slide 39

Slide 39 text

;Ή;Ή 6TF$BTFͷमਖ਼

Slide 40

Slide 40 text

class FetchMakerAccountPaginate { protected $maker_account_repo; public function __construct(MakerAccountRepository $maker_account_repo) { $this->maker_account_repo = $maker_account_repo; } public function __invoke(MakerAccountId $maker_account_id) { return $this->maker_account_repo ->findByMakerIdAsModel($maker_id) ->domainPaginate(); } } 6TF$BTFʢमਖ਼ޙʣ ҰͭͷৼΔ෣͍ͷΈʹ੍ݶ

Slide 41

Slide 41 text

class MakerAccountController extends Controller { public function index(FetchMakerAccountList $usecase) { $maker_accounts = $usecase(MakerId::of(auth()->maker->id())); return response()->json($maker_accounts); } } $POUSPMMFS ར༻

Slide 42

Slide 42 text

ࠓ೔͓࿩ͨ͜͠ͱ -BSBWFMͰ%%%Λ࣮ફ͢Δ·Ͱͷաఔ %%%Λ࣮ફ͢Δ͜ͱͰԿ͕มΘͬͨʁ Ұ౓Ͱ͸ແཧͰ΋ɺগͣͭ͠ͳΒ࣮ફͰ͖Δ ࢓༷ͷ೺Ѳ͕͠΍͘͢ͳͬͨ ϏδωεαΠυͱͷೝ͕ࣝͱΓ΍͘͢ͳͬͨ ઃܭ ࣮૷ ྑ͍ϧʔϓ

Slide 43

Slide 43 text

͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠

Slide 44

Slide 44 text

ฉ͖͍ͨ͜ͱ w &OUJUZ͕&OUJUZΛ࣋ͭ৔߹ͷύϑΥʔϚϯεͷ໰୊఺ w όϦσʔγϣϯ͸Ͳ͜Ͱ࣋ͨͤΔ΂͖͔ w ࣮ࡍ΍ͬͯΈͯɺԿ͕ྑ͔ͬͨͷ͔