Abstract_Repository.php
3 years ago
Ad_Repository.php
3 years ago
Group_Repository.php
2 years ago
Placement_Type.php
2 years ago
Placement_Type_Options.php
3 years ago
Ad_Repository.php
38 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Advanced_Ads; |
| 4 | |
| 5 | /** |
| 6 | * Ad Repository/Factory class. |
| 7 | * Ensures every ad is only set-up once and the same instance is re-used within one request. |
| 8 | */ |
| 9 | class Ad_Repository extends Abstract_Repository { |
| 10 | /** |
| 11 | * Array to hold the \Advanced_Ads_Ad objects, indexed by id. |
| 12 | * |
| 13 | * @var array |
| 14 | */ |
| 15 | protected static $repo = []; |
| 16 | |
| 17 | /** |
| 18 | * Get the ad object from the repository. Create and add it, if it doesn't exist. |
| 19 | * If the passed id is not an ad, return the created ad object without adding it to the repository. |
| 20 | * This behavior prevents breaking changes. |
| 21 | * |
| 22 | * @param int $id The ad id to look for. |
| 23 | * |
| 24 | * @return \Advanced_Ads_Ad |
| 25 | */ |
| 26 | public static function get( int $id ): \Advanced_Ads_Ad { |
| 27 | if ( ! self::has( $id ) ) { |
| 28 | $ad = new \Advanced_Ads_Ad( $id ); |
| 29 | if ( ! $ad->is_ad ) { |
| 30 | return $ad; |
| 31 | } |
| 32 | self::$repo[ $id ] = $ad; |
| 33 | } |
| 34 | |
| 35 | return self::$repo[ $id ]; |
| 36 | } |
| 37 | } |
| 38 |