if ( ! is_string($id) || trim($id) === '') { throw new \InvalidArgumentException( 'The product ID must be a non-empty value' ); } // do something to find ... } public static function update($id, array $data) { if ( ! is_string($id) || trim($id) === '') { throw new \InvalidArgumentException( 'The product ID must be a non-empty value' ); } // do something to update ... } } Id possui um comportamento duplicidade de código
__construct($value) { if ( ! is_string($value) || trim($value) === '') { throw new \InvalidArgumentException( 'The ID must be a non-empty string' ); } $this->value = $value; } public function getValue() { return $this->value; } }
__construct($value) { if ( ! filter_var($value, FILTER_VALIDATE_EMAIL)) { throw new \InvalidArgumentException( 'E-mail must be a valid format' ); } $this->value = $value; } public function getValue() { return $this->value; } }
$translator; protected $entityManager; protected $imageCropper; } EXEMPLO 1 Interação com BD deve estar em $userService Pode ser usado um event listener
$account = BankAccount::findOrFail($accountId); $account->balance = $account->balance + $amount; $account->save(); // do something to notify the client } public function withdraw($accountId, $amount) { $account = BankAccount::findOrFail($accountId); $account->balance = $account->balance - $amount; $account->save(); // do something to notify the client } }
$account = BankAccount::findOrFail($accountId); $account->deposit($amount); $account->save(); // do something to notify the client } public function withdraw($accountId, $amount) { $account = BankAccount::findOrFail($accountId); $account->withdraw($amount); $account->save(); // do something to notify the client } }