Skip to content

Instantly share code, notes, and snippets.

@Ragazzo
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ragazzo/b7637fa23e379391d022 to your computer and use it in GitHub Desktop.
Save Ragazzo/b7637fa23e379391d022 to your computer and use it in GitHub Desktop.
  • stateless free side functions
  • note about anemic models (entities)
  • example of services (generator service should be noted too)

BalanceService

/**
 * 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,
        ]);
    }

}

PurchaseService

/**
 * operation `checkout` that does not belong to any entity modelled as service according to UL
 */
order = PurchaseService->checkout(cart/offer)

RegistrationService

user = RegistrationService->registerNewUser(array $user) // call factory return new user

PermissionsService

content.domain.model.permissions.PermissionsService->can(User $user, $action, array $source)

PasswordService

account.domain.model.user.PasswordService->verify() / ->encrypt()

OrderIndentityService

services can generate identities IOrderIndentityService->nextIdentity() / ->identityFrom(array $information); ITicketIdentityService->generateFrom(array $data) -> ['flightNumber'], ['flightArrival'], ['flightDeparture'], ['timeArrival'], ['timeDeparture']

EventsService

common.domain.model.events.EventsService->trigger() / subscribe()

CodeGeneratingService

/**
 * 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()
    {
        /**
         *
         *
         */
    }

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment