{"id":20162472,"url":"https://github.com/willavelar/php-design-pattern-behavioral-command","last_synced_at":"2025-03-03T02:45:10.298Z","repository":{"id":203160121,"uuid":"696394501","full_name":"willavelar/php-design-pattern-behavioral-command","owner":"willavelar","description":"a simple php example about the behavioral pattern: Command","archived":false,"fork":false,"pushed_at":"2023-10-23T17:47:58.000Z","size":7,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-13T14:19:11.564Z","etag":null,"topics":["behavioral-patterns","command","design-patterns","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/willavelar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-09-25T16:51:26.000Z","updated_at":"2023-10-31T22:10:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"c3f1145d-7ace-463c-94a7-37213e9e4b5f","html_url":"https://github.com/willavelar/php-design-pattern-behavioral-command","commit_stats":null,"previous_names":["willavelar/php-design-partner-behavioral-command","willavelar/php-design-pattern-behavioral-command"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willavelar%2Fphp-design-pattern-behavioral-command","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willavelar%2Fphp-design-pattern-behavioral-command/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willavelar%2Fphp-design-pattern-behavioral-command/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willavelar%2Fphp-design-pattern-behavioral-command/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/willavelar","download_url":"https://codeload.github.com/willavelar/php-design-pattern-behavioral-command/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241600484,"owners_count":19988713,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["behavioral-patterns","command","design-patterns","php"],"created_at":"2024-11-14T00:25:18.018Z","updated_at":"2025-03-03T02:45:10.281Z","avatar_url":"https://github.com/willavelar.png","language":"PHP","readme":"## Command\r\n\r\nCommand is a behavioral design pattern that turns a request into a stand-alone object that contains all information about the request.\r\n\r\n-----\r\n\r\nWe need to generate a new order and thus execute everything that comes with it, such as sending emails\r\n\r\n### The problem\r\n\r\nIf we need to use request generation elsewhere, such as on a web page, or in an api, we would have to repeat the code to execute the same command.\r\n\r\n```php\r\n\u003c?php\r\nclass Budget \r\n{\r\n    public float $value;\r\n    public int $items;\r\n}\r\n```\r\n```php\r\n\u003c?php\r\nclass Order\r\n{\r\n    public string $customerName;\r\n    public \\DateTimeInterface $finalizationDate;\r\n    public Budget $budget;\r\n}\r\n```\r\n```php\r\n\u003c?php\r\n$budgetValue = $argv[1];\r\n$items = $argv[2];\r\n$customereName = $argv[3];\r\n\r\n$budget =  new Budget();\r\n\r\n$budget-\u003eitems = $items;\r\n$budget-\u003evalue = $budgetValue;\r\n\r\n$order = new Order();\r\n$order-\u003efinalizationDate = new DateTimeImmutable();\r\n$order-\u003ecustomerName = $customereName;\r\n$order-\u003ebudget = $budget;\r\n\r\necho \"create new order in database\" . PHP_EOL;\r\necho \"send email to customer\" . PHP_EOL;\r\n```\r\n\r\n### Not good solution\r\n\r\nWe can make a class, which will have the command to execute the request within a function, which can be called from several different places\r\n\r\n```php\r\n\u003c?php\r\nclass GenerateOrder\r\n{\r\n    private float $budgetValue;\r\n    private int $items;\r\n    private string $customereName;\r\n\r\n    public function __construct(float $budgetValue, int $items, string $customereName)\r\n    {\r\n        $this-\u003ebudgetValue = $budgetValue;\r\n        $this-\u003eitems = $items;\r\n        $this-\u003ecustomereName = $customereName;\r\n    }\r\n\r\n    public function execute()\r\n    {\r\n        $budget =  new Budget();\r\n\r\n        $budget-\u003eitems = $this-\u003eitems;\r\n        $budget-\u003evalue = $this-\u003ebudgetValue;\r\n\r\n        $order = new Order();\r\n        $order-\u003efinalizationDate = new \\DateTimeImmutable();\r\n        $order-\u003ecustomerName = $this-\u003ecustomereName;\r\n        $order-\u003ebudget = $budget;\r\n\r\n        echo \"create new order in database\" . PHP_EOL;\r\n        echo \"send email to customer\" . PHP_EOL;\r\n    }\r\n}\r\n```\r\n\r\n### The solution \r\n\r\nNow, using the Command design patern, we create a handler to be able to execute a request generation with dependency injection, not needing to know from where, or what abstraction, being able to execute it without getting attached to rules that are not necessary for it to get stuck.\r\n\r\n```php\r\n\u003c?php\r\nclass GenerateOrder\r\n{\r\n    private float $budgetValue;\r\n    private int $items;\r\n    private string $customereName;\r\n\r\n    public function __construct(float $budgetValue, int $items, string $customereName)\r\n    {\r\n        $this-\u003ebudgetValue = $budgetValue;\r\n        $this-\u003eitems = $items;\r\n        $this-\u003ecustomereName = $customereName;\r\n    }\r\n\r\n    public function getBudgetValue(): float\r\n    {\r\n        return $this-\u003ebudgetValue;\r\n    }\r\n\r\n    public function getItems(): int\r\n    {\r\n        return $this-\u003eitems;\r\n    }\r\n\r\n    public function getCustomereName(): string\r\n    {\r\n        return $this-\u003ecustomereName;\r\n    }\r\n\r\n}\r\n```\r\n\r\n```php\r\n\u003c?php\r\nclass GenerateOrderHandler\r\n{\r\n    public function __construct(/* OrderRepository, MailService */)\r\n    {\r\n    }\r\n\r\n    public function execute(GenerateOrder $generateOrder)\r\n    {\r\n        $budget =  new Budget();\r\n\r\n        $budget-\u003eitems = $generateOrder-\u003egetItems();\r\n        $budget-\u003evalue = $generateOrder-\u003egetBudgetValue();\r\n\r\n        $order = new Order();\r\n        $order-\u003efinalizationDate = new \\DateTimeImmutable();\r\n        $order-\u003ecustomerName = $generateOrder-\u003egetCustomereName();\r\n        $order-\u003ebudget = $budget;\r\n\r\n        // PedidoRepository\r\n        echo \"create new order in database\" . PHP_EOL;\r\n\r\n        // MailService\r\n        echo \"send email to customer\" . PHP_EOL;\r\n    }\r\n}\r\n```\r\n\r\n-----\r\n\r\n### Installation for test\r\n\r\n![PHP Version Support](https://img.shields.io/badge/php-7.4%2B-brightgreen.svg?style=flat-square) ![Composer Version Support](https://img.shields.io/badge/composer-2.2.9%2B-brightgreen.svg?style=flat-square)\r\n\r\n```bash\r\ncomposer install\r\n```\r\n\r\n```bash\r\nphp wrong/test.php {budgetValue} {items} {customereName}\r\nphp wrong2/test.php {budgetValue} {items} {customereName}\r\nphp right/test.php {budgetValue} {items} {customereName}\r\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillavelar%2Fphp-design-pattern-behavioral-command","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwillavelar%2Fphp-design-pattern-behavioral-command","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillavelar%2Fphp-design-pattern-behavioral-command/lists"}