- stateless free side functions
- note about anemic models (entities)
- example of services (generator service should be noted too)
/**
* BalanceService class file.
*
*/
namespace Marketing\Domain\Model\Client;
use Marketing\Domain\Model\Client\Client;
use Marketing\Domain\Model\Client\CanGenerateCouponFromBalanceSpecification;
use Core\Validation\Exception as ValidationException;
/**
*
*
*
*/
class BalanceService
{
/**
* @var \Marketing\Domain\Model\Coupon\ICodeGeneratingService
*/
private $couponCodeGeneratingService;
/**
* @var \Marketing\Domain\Model\Coupon\CouponFactory
*/
private $couponFactory;
/**
*
* @param array $config
*/
public function __construct(array $config)
{
$this->couponCodeGeneratingService = $config['couponCodeGeneratingService'];
$this->couponFactory = $config['couponFactory'];
}
/**
*
* @param Client $client
* @param integer $amount
* @return \Marketing\Domain\Model\Coupon\FixedAmountCouponPrivate
*/
public function generateCouponFromBalance(Client $client, $amount)
{
$specification = new CanGenerateCouponFromBalanceSpecification($amount);
if (!$specification->isSatisfiedBy($client)) {
throw new ValidationException('Unable to generate coupon for client with current balance. Not enough money to cover amount.');
}
$client->substractGeneratedCoupon($amount);
return $this->couponFactory->createFixedAmountPrivate([
'code' => $this->couponCodeGeneratingService->generate(),
'status' => true,
'amount' => $amount,
]);
}
}
/**
* operation `checkout` that does not belong to any entity modelled as service according to UL
*/
order = PurchaseService->checkout(cart/offer)
user = RegistrationService->registerNewUser(array $user) // call factory return new user
content.domain.model.permissions.PermissionsService->can(User $user, $action, array $source)
account.domain.model.user.PasswordService->verify() / ->encrypt()
services can generate identities IOrderIndentityService->nextIdentity() / ->identityFrom(array $information); ITicketIdentityService->generateFrom(array $data) -> ['flightNumber'], ['flightArrival'], ['flightDeparture'], ['timeArrival'], ['timeDeparture']
common.domain.model.events.EventsService->trigger() / subscribe()
/**
* CodeGeneratingService class file.
*
*/
namespace Marketing\Domain\Model\Coupon;
use Marketing\Domain\Model\Coupon\ICodeGeneratingService;
use Marketing\Domain\Model\Coupon\ICouponRepository;
/**
*
*
*
*/
class CodeGeneratingService implements ICodeGeneratingService
{
/**
* @var ICouponRepository
*/
private $couponRepository;
/**
*
* @param array $config
*/
public function __construct(array $config)
{
$this->couponRepository = $config['couponRepository'];
}
/**
* {@inheritDoc}
*/
public function generate()
{
while (true) {
$newCode = $this->generateRandomCode();
$existingCoupon = $this->couponRepository->findByCode($newCode);
if ($existingCoupon === null) {
return $newCode;
}
}
}
/**
* @return string
*/
protected function generateRandomCode()
{
/**
*
*
*/
}
}