]> _ Git - physioassist-wordpress.git/blob
7ce7f45c739caab18e21a25164efd2241047cd4e
[physioassist-wordpress.git] /
1 <?php
2
3 class OTGS_Installer_Repositories {
4
5         private $installer;
6         private $repositories;
7         private $repository_factory;
8         private $subscription_factory;
9
10         public function __construct(
11                 WP_Installer $installer,
12                 OTGS_Installer_Repository_Factory $repository_factory,
13                 OTGS_Installer_Subscription_Factory $subscription_factory
14         ) {
15                 $this->repository_factory   = $repository_factory;
16                 $this->subscription_factory = $subscription_factory;
17                 $this->installer            = $installer;
18                 $settings                   = $this->installer->get_settings();
19                 $this->repositories         = $this->get_repositories( $settings['repositories'] );
20         }
21
22         public function get_all() {
23                 return $this->repositories;
24         }
25
26         private function get_repositories( $setting_repositories ) {
27                 $repositories = array();
28
29                 foreach ( $setting_repositories as $id => $repository ) {
30                         $subscription = isset( $repository['subscription']['data'] )
31                                 ? $this->subscription_factory->create( $repository['subscription'] )
32                                 : null;
33
34                         $setting_repositories = $this->installer->get_repositories();
35
36                         $api_url = isset($setting_repositories[ $id ]['api-url']) ? $setting_repositories[ $id ]['api-url'] : null;
37                         $packages             = $this->get_packages( $repository );
38                         $repositories[] = $this->repository_factory->create_repository( array(
39                                         'id'            => $id,
40                                         'subscription'  => $subscription,
41                                         'packages'      => $packages,
42                                         'product_name'  => $repository['data']['product-name'],
43                                         'api_url'       => $api_url
44                                 )
45                         );
46                 }
47
48                 return $repositories;
49         }
50
51         private function get_packages( $repository ) {
52                 $packages = array();
53
54                 foreach ( $repository['data']['packages'] as $package_key => $package ) {
55                         $products = $this->get_products( $package );
56
57                         $packages[] = $this->repository_factory->create_package( array(
58                                 'key'         => $package_key,
59                                 'id'          => $package['id'],
60                                 'name'        => $package['name'],
61                                 'description' => $package['description'],
62                                 'image_url'   => $package['image_url'],
63                                 'order'       => $package['order'],
64                                 'parent'      => $package['parent'],
65                                 'products'    => $products,
66                         ) );
67                 }
68
69                 return $packages;
70         }
71
72         private function get_products( $package ) {
73                 $products = array();
74
75                 foreach ( $package['products'] as $product_key => $product ) {
76                         $products[] = $this->repository_factory->create_product( array(
77                                 'id'                           => $product_key,
78                                 'name'                         => $product['name'],
79                                 'description'                  => $product['description'],
80                                 'price'                        => $product['price'],
81                                 'subscription_type'            => $product['subscription_type'],
82                                 'subscription_type_text'       => $product['subscription_type_text'],
83                                 'subscription_info'            => $product['subscription_info'],
84                                 'subscription_type_equivalent' => $product['subscription_type_equivalent'],
85                                 'url'                          => $product['url'],
86                                 'renewals'                     => $product['renewals'],
87                                 'upgrades'                     => $product['upgrades'],
88                                 'plugins'                      => $product['plugins'],
89                                 'downloads'                    => $product['downloads'],
90                         ) );
91                 }
92
93                 return $products;
94         }
95
96         /**
97          * @param $id
98          *
99          * @return null|OTGS_Installer_Repository
100          */
101         public function get( $id ) {
102                 foreach ( $this->repositories as $repository ) {
103                         if ( $id === $repository->get_id() ) {
104                                 return $repository;
105                         }
106                 }
107
108                 return null;
109         }
110
111         public function refresh( $bypass_bucket = false ) {
112                 return $this->installer->refresh_repositories_data( $bypass_bucket );
113         }
114
115         public function save_subscription( OTGS_Installer_Repository $repository ) {
116                 $subscription = $repository->get_subscription();
117                 unset( $this->installer->settings['repositories'][ $repository->get_id() ]['subscription'] );
118
119                 if ( $subscription ) {
120                         $this->installer->settings['repositories'][ $repository->get_id() ]['subscription'] = array(
121                                 'key'           => $subscription->get_site_key(),
122                                 'data'          => $subscription->get_data(),
123                                 'registered_by' => $subscription->get_registered_by(),
124                                 'site_url'      => $subscription->get_site_url(),
125                         );
126                 }
127
128                 $this->installer->save_settings();
129         }
130 }