33namespace Nails \Cdn \Service ;
44
55use Nails \Cdn \Constants ;
6+ use Nails \Cdn \Exception \CdnException ;
67use Nails \Common \Service \Session ;
8+ use Nails \Config ;
79
810class MediaManager
911{
@@ -14,24 +16,96 @@ public function __construct(
1416 ) {
1517 }
1618
19+ public function getVersions (): array
20+ {
21+ return [
22+ Constants::MEDIA_MANAGER_V1 ,
23+ Constants::MEDIA_MANAGER_V2 ,
24+ ];
25+ }
26+
27+ public function isValidVersion (int $ version ): bool
28+ {
29+ return in_array ($ version , $ this ->getVersions (), true );
30+ }
31+
32+ public function getEnabledVersions (): array
33+ {
34+ $ enabled = Config::get ('CDN_MEDIA_MANAGER_ENABLE ' );
35+ if (!is_array ($ enabled )) {
36+ $ enabled = [$ enabled ];
37+ }
38+
39+ $ enabled = array_values (
40+ array_filter (
41+ $ enabled ,
42+ fn (int $ version ) => $ this ->isValidVersion ($ version )
43+ )
44+ );
45+
46+ return empty ($ enabled )
47+ ? $ this ->getVersions ()
48+ : $ enabled ;
49+ }
50+
51+ public function isVersionEnabled (int $ version ): bool
52+ {
53+ return in_array ($ version , $ this ->getEnabledVersions (), true );
54+ }
55+
56+ /**
57+ * @throws CdnException
58+ */
1759 public function setDefault (int $ version ): static
1860 {
61+ if (!$ this ->isValidVersion ($ version )) {
62+ throw new CdnException ('Invalid version provided ' );
63+ } elseif (!$ this ->isVersionEnabled ($ version )) {
64+ throw new CdnException (sprintf ('Version %d is not enabled ' , $ version ));
65+ }
66+
1967 $ this ->session ->setUserData (static ::SESSION_KEY_DEFAULT , $ version );
2068 return $ this ;
2169 }
2270
23- public function getUrl (array $ query = [], ?int $ version = null ): string
71+ /**
72+ * @throws CdnException
73+ */
74+ public function getUrl (array $ query = [], string $ path = '' , ?int $ version = null ): string
2475 {
76+ // If an explicitly passed version is not enabled, complain
77+ if ($ version && !$ this ->isVersionEnabled ($ version )) {
78+ throw new CdnException (sprintf ('Version %d is not enabled ' , $ version ));
79+ }
2580
26- $ sBaseUrl = match ($ version ?? $ this ->session ->getUserData (static ::SESSION_KEY_DEFAULT )) {
81+ if (!$ version && $ this ->session ->getUserData (static ::SESSION_KEY_DEFAULT )) {
82+ $ version = $ this ->session ->getUserData (static ::SESSION_KEY_DEFAULT );
83+ if (!$ this ->isVersionEnabled ($ version )) {
84+ // Version stored in session is no longer valid, default to the first enabled manager
85+ $ enabledVersions = $ this ->getEnabledVersions ();
86+ $ version = reset ($ enabledVersions );
87+ $ this ->setDefault ($ version );
88+ }
89+ }
90+
91+ $ managerUrl = match ($ version ) {
92+ Constants::MEDIA_MANAGER_V1 => Constants::MEDIA_MANAGER_V1_URL ,
2793 Constants::MEDIA_MANAGER_V2 => Constants::MEDIA_MANAGER_V2_URL ,
28- default => Constants:: MEDIA_MANAGER_V1_URL
94+ default => null ,
2995 };
3096
97+ if (!$ managerUrl ) {
98+ throw new CdnException ('Unable to determine media manager url ' );
99+ }
100+
101+ if (trim ($ path )) {
102+ $ managerUrl .= '/ ' . ltrim ($ path , '/ ' );
103+ }
104+
31105 if ($ query ) {
32- $ sBaseUrl .= '? ' . http_build_query ($ query );
106+ $ managerUrl .= '? ' . http_build_query ($ query );
33107 }
34108
35- return siteUrl ($ sBaseUrl );
109+ return siteUrl ($ managerUrl );
36110 }
37111}
0 commit comments