Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Refactoring & Tests
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Gilberto Bueno
June 01, 2017
Programming
150
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Refactoring & Tests
Using tests during refactoring process
Gilberto Bueno
June 01, 2017
More Decks by Gilberto Bueno
See All by Gilberto Bueno
Docker for beginners
gilbertojb
0
87
Object Calisthenics
gilbertojb
0
110
Laravel Tips & Tricks
gilbertojb
1
140
Other Decks in Programming
See All in Programming
Agentic Software Factoryに、すごく賢いIF文を。 / super smart IF statement into the Agentic Software Factory.
rkaga
8
2.2k
[2026-09-26]空論ジェネリックプロセス~テスト資産とAIで紡ぐ、再現可能なパフォーマンスチューニングの話~
tosite
0
270
機械に任せるテスト、人が見るテスト ⽣成AIで引き直した、運⽤保守フェーズの線引き
dske104
0
190
現場と育てるRailsモデリング 〜Spreadsheetから始めたモデル設計〜
siroemk
2
590
TechTown - Creating Composable Callables in Contemporary C++
rollbear
0
130
S3 TablesのIcebergデータに、AIエージェントからアクセスしてみた / 20261016developersio2026joetsu-accessing-iceberg-in-s3tables-via-agent
kasacchiful
PRO
0
150
作って学ぶSpring BootのAuto Configuration / JJUG Night Seminar 2026-10-19
rshindo
0
140
すこし踏み込む CancellationToken
htkym
2
1.7k
Long Living Tokens, Die, Die, Die! (or: The One Ring is Banned in Modern)
sylph01
0
300
Simple Storage Service(S3) is not simple
iwatsukayura
0
290
contenteditable と日本語入力に向き合う
colorful12
0
200
golang-bob.pdf
iwatsukayura
0
130
Featured
See All Featured
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
570
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.6k
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k
Mobile First: as difficult as doing things right
swwweet
225
10k
Designing for Timeless Needs
cassininazir
1
530
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.2k
Navigating Team Friction
lara
192
16k
Building Flexible Design Systems
yeseniaperezcruz
330
41k
Ethics towards AI in product and experience design
skipperchong
2
430
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
330
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
620
We Are The Robots
honzajavorek
0
400
Transcript
Refactoring & Tests Usando testes durante o processo de refatoração
refactoring Definição da Web Refatoração é o processo de modificar
um sistema de software para melhorar a estrutura interna do código sem alterar seu comportamento externo… http://pt.wikipedia.org/wiki/Refactoring
Problema 1
protected function saveCustomer(Klarna_Checkout_Order $order) { $billingAddress = $this->customerAddressRepository->findWhere([ 'customer_id' =>
$customer->id, 'type' => 'billing' ])->first(); if (is_null($billingAddress)) { $this->customerAddressRepository->create([ 'type' => 'billing', 'address' => $order['billing_address']['street_address'], 'zipcode' => $order['billing_address']['postal_code'], 'city' => $order['billing_address']['city'], 'country' => $order['billing_address']['country'], 'customer_id' => $customer->id ]); } else { $this->customerAddressRepository->update([ 'type' => 'billing', 'address' => $order['billing_address']['street_address'], 'zipcode' => $order['billing_address']['postal_code'], 'city' => $order['billing_address']['city'], 'country' => $order['billing_address']['country'], 'customer_id' => $customer->id ], $billingAddress->id); } }
protected function saveCustomer(Klarna_Checkout_Order $order) { $billingAddress = $this->customerAddressRepository->findWhere([ 'customer_id' =>
$customer->id, 'type' => 'billing' ])->first(); if (is_null($billingAddress)) { $this->customerAddressRepository->create([ 'type' => 'billing', 'address' => $order['billing_address']['street_address'], 'zipcode' => $order['billing_address']['postal_code'], 'city' => $order['billing_address']['city'], 'country' => $order['billing_address']['country'], 'customer_id' => $customer->id ]); } else { $this->customerAddressRepository->update([ 'type' => 'billing', 'address' => $order['billing_address']['street_address'], 'zipcode' => $order['billing_address']['postal_code'], 'city' => $order['billing_address']['city'], 'country' => $order['billing_address']['country'], 'customer_id' => $customer->id ], $billingAddress->id); } }
protected function saveCustomer(Klarna_Checkout_Order $order) { $billingAddress = $this->customerAddressRepository->findWhere([ 'customer_id' =>
$customer->id, 'type' => 'billing' ])->first(); if (is_null($billingAddress)) { $this->customerAddressRepository->create([ 'type' => 'billing', 'address' => $order['billing_address']['street_address'], 'zipcode' => $order['billing_address']['postal_code'], 'city' => $order['billing_address']['city'], 'country' => $order['billing_address']['country'], 'customer_id' => $customer->id ]); } else { $this->customerAddressRepository->update([ 'type' => 'billing', 'address' => $order['billing_address']['street_address'], 'zipcode' => $order['billing_address']['postal_code'], 'city' => $order['billing_address']['city'], 'country' => $order['billing_address']['country'], 'customer_id' => $customer->id ], $billingAddress->id); } }
protected function saveCustomer(Klarna_Checkout_Order $order) { $billingAddress = $this->customerAddressRepository->findWhere([ 'customer_id' =>
$customer->id, 'type' => 'billing' ])->first(); if (is_null($billingAddress)) { $this->customerAddressRepository->create([ 'type' => 'billing', 'address' => $order['billing_address']['street_address'], 'zipcode' => $order['billing_address']['postal_code'], 'city' => $order['billing_address']['city'], 'country' => $order['billing_address']['country'], 'customer_id' => $customer->id ]); } else { $this->customerAddressRepository->update([ 'type' => 'billing', 'address' => $order['billing_address']['street_address'], 'zipcode' => $order['billing_address']['postal_code'], 'city' => $order['billing_address']['city'], 'country' => $order['billing_address']['country'], 'customer_id' => $customer->id ], $billingAddress->id); } }
None
https://github.com/laravel/framework
https://goo.gl/05km18
None
None
Solução 1
https://laravel.com/docs/master/eloquent
None
<?php namespace Hydra\Athena\Test\Functional\Service; class CustomerAddressTest extends TestCase { /** *
@test */ public function save_new_customer_billing_address_if_not_exist() { $customer = $this->createdCustomer(); $attributes = $this->customerAddressStub(); $billingAddress = $this->service->saveBillingAddress($customer, $attributes); $this->assertEquals($billingAddress->address, $attributes['address']); $this->assertEquals($billingAddress->zipcode, $attributes['zipcode']); $this->assertEquals($billingAddress->city, $attributes['city']); $this->assertEquals($billingAddress->country, $attributes['country']); } }
/** * @return array */ private function customerAddressStub() { return
[ 'address' => $this->faker->streetAddress, 'zipcode' => $this->faker->postcode, 'city' => $this->faker->city, 'country' => $this->faker->country, ]; } /** * @return Customer */ private function createdCustomer() { $customer = new Customer(); return $customer->create([ 'name' => $this->faker->name, 'email' => $this->faker->email, 'phone' => $this->faker->phoneNumber, 'birthday' => $this->faker->dateTimeThisCentury->format('Y-m-d'), ]); }
<?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function
saveBillingAddress($customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrCreate([ 'customer_id' => $customer->id, 'type' => 'billing' ]); $model->fill($attributes); $model->save(); return $model; } }
None
<?php namespace Hydra\Athena\Validator; /** * Class CustomerAddress * @package Hydra\Athena\Validator
*/ class CustomerAddress extends Validator { public $rules = [ 'address' => 'required' ]; }
<?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function
saveBillingAddress($customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrCreate([ 'customer_id' => $customer->id, 'type' => 'billing', 'address' => $attributes['address'] ]); $model->fill($attributes); $model->save(); return $model; } }
None
<?php namespace Hydra\Athena\Test\Functional\Service; class CustomerAddressTest extends TestCase { /** *
@test */ public function update_customer_billing_address_if_exists() { $customer = $this->createdCustomer(); $attributes = $this->customerAddressStub(); $this->service->saveBillingAddress($customer, $attributes); $attributes['city'] = 'Orlando'; $billingAddress = $this->service->saveBillingAddress($customer, $attributes); $this->assertEquals($billingAddress->address, $attributes['address']); $this->assertEquals($billingAddress->zipcode, $attributes['zipcode']); $this->assertEquals($billingAddress->country, $attributes['country']); $this->assertEquals($billingAddress->city, 'Orlando'); } }
<?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function
saveBillingAddress($customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrCreate([ 'customer_id' => $customer->id, 'type' => 'billing', 'address' => $attributes['address'] // new address ]); $model->fill($attributes); $model->save(); return $model; } }
<?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function
saveBillingAddress($customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrCreate([ 'customer_id' => $customer->id, 'type' => 'billing', 'address' => $attributes['address'] // new address ]); $model->fill($attributes); $model->save(); return $model; } }
<?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function
saveBillingAddress($customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([ 'customer_id' => $customer->id, 'type' => 'billing' ]); $model->fill($attributes); $model->save(); return $model; } }
None
Problema 2
protected function saveCustomer(Klarna_Checkout_Order $order) { $shippingAddress = $this->customerAddressRepository->findWhere([ 'customer_id' =>
$customer->id, 'type' => 'shipping' ])->first(); if (is_null($shippingAddress)) { $this->customerAddressRepository->create([ 'type' => 'shipping', 'address' => $order['shipping_address']['street_address'], 'zipcode' => $order['shipping_address']['postal_code'], 'city' => $order['shipping_address']['city'], 'country' => $order['shipping_address']['country'], 'customer_id' => $customer->id ]); } else { $this->customerAddressRepository->update([ 'type' => 'shipping', 'address' => $order['shipping_address']['street_address'], 'zipcode' => $order['shipping_address']['postal_code'], 'city' => $order['shipping_address']['city'], 'country' => $order['shipping_address']['country'], 'customer_id' => $customer->id ], $billingAddress->id); } }
protected function saveCustomer(Klarna_Checkout_Order $order) { $shippingAddress = $this->customerAddressRepository->findWhere([ 'customer_id' =>
$customer->id, 'type' => 'shipping' ])->first(); if (is_null($shippingAddress)) { $this->customerAddressRepository->create([ 'type' => 'shipping', 'address' => $order['shipping_address']['street_address'], 'zipcode' => $order['shipping_address']['postal_code'], 'city' => $order['shipping_address']['city'], 'country' => $order['shipping_address']['country'], 'customer_id' => $customer->id ]); } else { $this->customerAddressRepository->update([ 'type' => 'shipping', 'address' => $order['shipping_address']['street_address'], 'zipcode' => $order['shipping_address']['postal_code'], 'city' => $order['shipping_address']['city'], 'country' => $order['shipping_address']['country'], 'customer_id' => $customer->id ], $billingAddress->id); } }
protected function saveCustomer(Klarna_Checkout_Order $order) { $shippingAddress = $this->customerAddressRepository->findWhere([ 'customer_id' =>
$customer->id, 'type' => 'shipping' ])->first(); if (is_null($shippingAddress)) { $this->customerAddressRepository->create([ 'type' => 'shipping', 'address' => $order['shipping_address']['street_address'], 'zipcode' => $order['shipping_address']['postal_code'], 'city' => $order['shipping_address']['city'], 'country' => $order['shipping_address']['country'], 'customer_id' => $customer->id ]); } else { $this->customerAddressRepository->update([ 'type' => 'shipping', 'address' => $order['shipping_address']['street_address'], 'zipcode' => $order['shipping_address']['postal_code'], 'city' => $order['shipping_address']['city'], 'country' => $order['shipping_address']['country'], 'customer_id' => $customer->id ], $billingAddress->id); } }
protected function saveCustomer(Klarna_Checkout_Order $order) { $shippingAddress = $this->customerAddressRepository->findWhere([ 'customer_id' =>
$customer->id, 'type' => 'shipping' ])->first(); if (is_null($shippingAddress)) { $this->customerAddressRepository->create([ 'type' => 'shipping', 'address' => $order['shipping_address']['street_address'], 'zipcode' => $order['shipping_address']['postal_code'], 'city' => $order['shipping_address']['city'], 'country' => $order['shipping_address']['country'], 'customer_id' => $customer->id ]); } else { $this->customerAddressRepository->update([ 'type' => 'shipping', 'address' => $order['shipping_address']['street_address'], 'zipcode' => $order['shipping_address']['postal_code'], 'city' => $order['shipping_address']['city'], 'country' => $order['shipping_address']['country'], 'customer_id' => $customer->id ], $billingAddress->id); } }
None
Solução 2
<?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function
saveShippingAddress($customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([ 'customer_id' => $customer->id, 'type' => 'shipping' ]); $model->fill($attributes); $model->save(); return $model; } }
<?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function
saveBillingAddress($customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([ 'customer_id' => $customer->id, 'type' => 'billing' ]); $model->fill($attributes); $model->save(); return $model; } public function saveShippingAddress($customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([ 'customer_id' => $customer->id, 'type' => 'shipping' ]); $model->fill($attributes); $model->save(); return $model; } }
<?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function
saveBillingAddress($customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([ 'customer_id' => $customer->id, 'type' => 'billing' ]); $model->fill($attributes); $model->save(); return $model; } public function saveShippingAddress($customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([ 'customer_id' => $customer->id, 'type' => 'shipping' ]); $model->fill($attributes); $model->save(); return $model; } }
public function saveBillingAddress($customer, $attributes) { return $this->saveAddress('billing', $customer, $attributes); }
public function saveShippingAddress($customer, $attributes) { return $this->saveAddress('shipping', $customer, $attributes); } private function saveAddress($type, $customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([ 'customer_id' => $customer->id, 'type' => $type ]); $model->fill($attributes); $model->save(); return $model; }
public function saveBillingAddress($customer, $attributes) { return $this->saveAddress('billing', $customer, $attributes); }
public function saveShippingAddress($customer, $attributes) { return $this->saveAddress('shipping', $customer, $attributes); } private function saveAddress($type, $customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([ 'customer_id' => $customer->id, 'type' => $type ]); $model->fill($attributes); $model->save(); return $model; }
public function saveBillingAddress($customer, $attributes) { return $this->saveAddress('billing', $customer, $attributes); }
public function saveShippingAddress($customer, $attributes) { return $this->saveAddress('shipping', $customer, $attributes); } private function saveAddress($type, $customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([ 'customer_id' => $customer->id, 'type' => $type ]); $model->fill($attributes); $model->save(); return $model; }
public function saveBillingAddress($customer, $attributes) { return $this->saveAddress('billing', $customer, $attributes); }
public function saveShippingAddress($customer, $attributes) { return $this->saveAddress('shipping', $customer, $attributes); } private function saveAddress($type, $customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([ 'customer_id' => $customer->id, 'type' => $type ]); $model->fill($attributes); $model->save(); return $model; }
/** * @test */ public function save_new_customer_shipping_address_if_not_exist() { $customer =
$this->createdCustomer(); $attributes = $this->customerAddressStub(); $billingAddress = $this->service->saveShippingAddress($customer, $attributes); $this->assertEquals($billingAddress->address, $attributes['address']); $this->assertEquals($billingAddress->zipcode, $attributes['zipcode']); $this->assertEquals($billingAddress->city, $attributes['city']); $this->assertEquals($billingAddress->country, $attributes['country']); } /** * @test */ public function update_customer_shipping_address_if_exists() { $customer = $this->createdCustomer(); $attributes = $this->customerAddressStub(); $this->service->saveShippingAddress($customer, $attributes); $attributes['city'] = 'Miami'; $billingAddress = $this->service->saveShippingAddress($customer, $attributes); $this->assertEquals($billingAddress->address, $attributes['address']); $this->assertEquals($billingAddress->zipcode, $attributes['zipcode']); $this->assertEquals($billingAddress->country, $attributes['country']); $this->assertEquals($billingAddress->city, 'Miami'); }
None
Solução Final
<?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { /** *
@var array */ protected $addressTypes = [ 'billing', 'shipping' ];
<?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { private function
saveAddress($type, $customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([ 'customer_id' => $customer->id, 'type' => $type ]); $model->fill($attributes); $model->save(); return $model; }
<?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function
__call($type, $arguments) { list($customer, $attributes) = $arguments; $typeName = substr(strtolower($type), 4); if (!in_array($typeName, $this->addressTypes)) { throw new InvalidAddressTypeException( "Address type '{$typeName}' is invalid" ); } return $this->saveAddress($typeName, $customer, $attributes); } http://goo.gl/jxRwic
<?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function
__call($type, $arguments) { list($customer, $attributes) = $arguments; $typeName = substr(strtolower($type), 4); if (!in_array($typeName, $this->addressTypes)) { throw new InvalidAddressTypeException( "Address type '{$typeName}' is invalid" ); } return $this->saveAddress($typeName, $customer, $attributes); } http://goo.gl/lzRI4Q
<?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function
__call($type, $arguments) { list($customer, $attributes) = $arguments; $typeName = substr(strtolower($type), 4); if (!in_array($typeName, $this->addressTypes)) { throw new InvalidAddressTypeException( "Address type '{$typeName}' is invalid" ); } return $this->saveAddress($typeName, $customer, $attributes); } http://goo.gl/mdH68i
<?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function
__call($type, $arguments) { list($customer, $attributes) = $arguments; $typeName = substr(strtolower($type), 4); if (!in_array($typeName, $this->addressTypes)) { throw new InvalidAddressTypeException( "Address type '{$typeName}' is invalid" ); } return $this->saveAddress($typeName, $customer, $attributes); } http://goo.gl/mdH68i saveBilling billing
<?php namespace Hydra\Athena\Service; class CustomerAddress extends Service { public function
__call($type, $arguments) { list($customer, $attributes) = $arguments; $typeName = substr(strtolower($type), 4); if (!in_array($typeName, $this->addressTypes)) { throw new InvalidAddressTypeException( "Address type '{$typeName}' is invalid" ); } return $this->saveAddress($typeName, $customer, $attributes); } http://goo.gl/Im7UHU
<?php namespace Hydra\Athena\Test\Functional\Service; class CustomerAddressTest extends TestCase { /** *
@test */ public function save_new_customer_billing_address_if_not_exist() { $customer = $this->createdCustomer(); $attributes = $this->customerAddressStub(); $billingAddress = $this->service->saveBilling($customer, $attributes); $this->assertEquals($billingAddress->address, $attributes['address']); $this->assertEquals($billingAddress->zipcode, $attributes['zipcode']); $this->assertEquals($billingAddress->city, $attributes['city']); $this->assertEquals($billingAddress->country, $attributes['country']); } }
<?php namespace Hydra\Athena\Test\Functional\Service; class CustomerAddressTest extends TestCase { /** *
@test */ public function update_customer_billing_address_if_exists() { $customer = $this->createdCustomer(); $attributes = $this->customerAddressStub(); $this->service->saveBilling($customer, $attributes); $attributes['city'] = 'Toronto'; $billingAddress = $this->service->saveBilling($customer, $attributes); $this->assertEquals($billingAddress->address, $attributes['address']); $this->assertEquals($billingAddress->zipcode, $attributes['zipcode']); $this->assertEquals($billingAddress->country, $attributes['country']); $this->assertEquals($billingAddress->city, 'Toronto'); } }
<?php namespace Hydra\Athena\Test\Functional\Service; class CustomerAddressTest extends TestCase { /** *
@test */ public function save_new_customer_shipping_address_if_not_exist() { $customer = $this->createdCustomer(); $attributes = $this->customerAddressStub(); $billingAddress = $this->service->saveShipping($customer, $attributes); $this->assertEquals($billingAddress->address, $attributes['address']); $this->assertEquals($billingAddress->zipcode, $attributes['zipcode']); $this->assertEquals($billingAddress->city, $attributes['city']); $this->assertEquals($billingAddress->country, $attributes['country']); } }
<?php namespace Hydra\Athena\Test\Functional\Service; class CustomerAddressTest extends TestCase { /** *
@test */ public function update_customer_shipping_address_if_exists() { $customer = $this->createdCustomer(); $attributes = $this->customerAddressStub(); $this->service->saveBilling($customer, $attributes); $attributes['city'] = 'Vancouver'; $billingAddress = $this->service->saveShipping($customer, $attributes); $this->assertEquals($billingAddress->address, $attributes['address']); $this->assertEquals($billingAddress->zipcode, $attributes['zipcode']); $this->assertEquals($billingAddress->country, $attributes['country']); $this->assertEquals($billingAddress->city, 'Vancouver'); } }
<?php namespace Hydra\Athena\Test\Functional\Service; class CustomerAddressTest extends TestCase { /** *
@test * @expectedException \Hydra\Athena\Exceptions\InvalidAddressTypeException */ public function throw_exception_if_address_type_is_invalid() { $customer = $this->createdCustomer(); $attributes = $this->customerAddressStub(); $this->service->saveWork($customer, $attributes); } }
None
protected function saveCustomer(Klarna_Checkout_Order $order) { . . . $billingAddress =
$this->customerAddressRepository->findWhere([ 'customer_id' => $customer->id, 'type' => 'billing' ])->first(); if (is_null($billingAddress)) { $this->customerAddressRepository->create([ 'type' => 'billing', 'address' => $order['billing_address']['street_address'], 'zipcode' => $order['billing_address']['postal_code'], 'city' => $order['billing_address']['city'], 'country' => $order['billing_address']['country'], 'customer_id' => $customer->id ]); } else { $this->customerAddressRepository->update([ 'type' => 'billing', 'address' => $order['billing_address']['street_address'], 'zipcode' => $order['billing_address']['postal_code'], 'city' => $order['billing_address']['city'], 'country' => $order['billing_address']['country'], 'customer_id' => $customer->id ], $billingAddress->id); } $shippingAddress = $this->customerAddressRepository->findWhere([ 'customer_id' => $customer->id, 'type' => 'shipping' ])->first(); if (is_null($shippingAddress)) { $this->customerAddressRepository->create([ 'type' => 'shipping', 'address' => $order['shipping_address']['street_address'], 'zipcode' => $order['shipping_address']['postal_code'], 'city' => $order['shipping_address']['city'], 'country' => $order['shipping_address']['country'], 'customer_id' => $customer->id ]); } else { $this->customerAddressRepository->update([ 'type' => 'shipping', 'address' => $order['shipping_address']['street_address'], 'zipcode' => $order['shipping_address']['postal_code'], 'city' => $order['shipping_address']['city'], 'country' => $order['shipping_address']['country'], 'customer_id' => $customer->id ], $billingAddress->id); } . . . } 49 loc 71 loc
private function saveAddress($type, $customer, $attributes) { $this->validator->validate($attributes); $model = $this->model->firstOrNew([
'customer_id' => $customer->id, 'type' => $type ]); $model->fill($attributes); $model->save(); return $model; } 11 loc
Obrigado.