-
-
Save anonymous/7c1e89dcd8056dad6857 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class BannableFactory { | |
/** | |
* @param User $user | |
* @return BannableUser | |
*/ | |
public function createBannableUser(User $user) { | |
return new BannableUser($user); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class BannableUser { | |
/** @var User */ | |
private $user; | |
/** | |
* @param User $user | |
*/ | |
public function __construct(User $user) { | |
$this->user = $user; | |
} | |
/** | |
* @return bool | |
*/ | |
public function isBanned() { | |
/* ... */ | |
} | |
/** | |
* @return bool | |
*/ | |
public function ban() { | |
return $this->setBanStatus(true); | |
} | |
/** | |
* @return bool | |
*/ | |
public function unban() { | |
return $this->setBanStatus(false); | |
} | |
/** | |
* @param bool $status | |
* @return bool Return true when the status change was successful regardless of the actual status and false if not | |
*/ | |
private function setBanStatus($status) { | |
/* ... */ | |
return $this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class BanUserCtrl { | |
/** @var BannableFactory */ | |
private $factory; | |
/** | |
* @param BannableFactory $factory | |
*/ | |
public function __construct(BannableFactory $factory) { | |
$this->factory = $factory; | |
} | |
/** | |
* @param User $user | |
* @return bool | |
*/ | |
public function banUser(User $user) { | |
$bannableUser = $this->factory->createBannableUser($user); | |
if(!$bannableUser->isBanned()) { | |
$success = $bannableUser->ban(); | |
return $success; | |
} | |
return false; | |
} | |
/** | |
* @param User $user | |
* @return bool | |
*/ | |
public function unbanUser(User $user) { | |
$bannableUser = $this->factory->createBannableUser($user); | |
if($bannableUser->isBanned()) { | |
$success = $bannableUser->unban(); | |
return $success; | |
} | |
return false; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class User { | |
/** @var int */ | |
private $id; | |
/** @var int */ | |
private $name; | |
/** | |
* @return int | |
*/ | |
public function getId() { | |
return $this->id; | |
} | |
/** | |
* @param int $id | |
* @return $this | |
*/ | |
public function setId($id) { | |
$this->id = $id; | |
return $this; | |
} | |
/** | |
* @return int | |
*/ | |
public function getName() { | |
return $this->name; | |
} | |
/** | |
* @param int $name | |
* @return $this | |
*/ | |
public function setName($name) { | |
$this->name = $name; | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment