{"id":20162480,"url":"https://github.com/willavelar/php-design-pattern-structural-proxy","last_synced_at":"2025-03-03T02:45:10.858Z","repository":{"id":208247543,"uuid":"721170532","full_name":"willavelar/php-design-pattern-structural-proxy","owner":"willavelar","description":"About a simple php example about the structural pattern: Proxy","archived":false,"fork":false,"pushed_at":"2023-11-20T13:59:12.000Z","size":3,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-13T14:19:13.896Z","etag":null,"topics":["design-patterns","php","proxy","structural-patterns"],"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-11-20T13:56:57.000Z","updated_at":"2023-11-20T13:58:28.000Z","dependencies_parsed_at":"2023-11-20T14:59:20.278Z","dependency_job_id":null,"html_url":"https://github.com/willavelar/php-design-pattern-structural-proxy","commit_stats":null,"previous_names":["willavelar/php-design-pattern-structural-proxy"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willavelar%2Fphp-design-pattern-structural-proxy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willavelar%2Fphp-design-pattern-structural-proxy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willavelar%2Fphp-design-pattern-structural-proxy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willavelar%2Fphp-design-pattern-structural-proxy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/willavelar","download_url":"https://codeload.github.com/willavelar/php-design-pattern-structural-proxy/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":["design-patterns","php","proxy","structural-patterns"],"created_at":"2024-11-14T00:25:19.683Z","updated_at":"2025-03-03T02:45:10.840Z","avatar_url":"https://github.com/willavelar.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Proxy\r\n\r\nProxy is a structural design pattern that lets you provide a substitute or placeholder for another object. A proxy controls access to the original object, allowing you to perform something either before or after the request gets through to the original object.\r\n\r\n-----\r\n\r\nWe need to create a discount calculator, where the calculation makes a request to an external API\r\n\r\n### The problem\r\n\r\nIf we do it this way, we are held hostage by the API if it takes too long\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 DiscountCalculator\r\n{\r\n    public function calc(Budget $budget) : float\r\n    {\r\n        // API Request\r\n        sleep(1);\r\n\r\n        $discountCalc = $budget-\u003evalue *  0.1;\r\n\r\n        (new RedisLog())-\u003esave($discountCalc);\r\n\r\n        return $discountCalc;\r\n    }\r\n}\r\n```\r\n```php\r\n\u003c?php\r\ninterface Log\r\n{\r\n    public function save(string $info) : void;\r\n}\r\n```\r\n```php\r\n\u003c?php\r\nclass RedisLog implements Log\r\n{\r\n    public function save(string $info): void\r\n    {\r\n        // TODO: Implement save() method.\r\n    }\r\n}\r\n```\r\n\r\n### The solution\r\n\r\nNow, using the Proxy pattern, we can store the value of the original object, not repeating it every time for the API\r\n\r\n```php\r\n\u003c?php\r\nclass DiscountCalculatorProxy extends DiscountCalculator\r\n{\r\n    private float $calcProxy = 0;\r\n    private DiscountCalculator $discountCalculator;\r\n\r\n    public function __construct(DiscountCalculator $discountCalculator)\r\n    {\r\n        $this-\u003ediscountCalculator = $discountCalculator;\r\n    }\r\n\r\n    public function calc(Budget $budget): float\r\n    {\r\n        if ($this-\u003ecalcProxy == 0) {\r\n            $this-\u003ecalcProxy = $this-\u003ediscountCalculator-\u003ecalc($budget);\r\n        }\r\n\r\n        return $this-\u003ecalcProxy;\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\r\nphp right/test.php\r\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillavelar%2Fphp-design-pattern-structural-proxy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwillavelar%2Fphp-design-pattern-structural-proxy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillavelar%2Fphp-design-pattern-structural-proxy/lists"}