| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace CSFCloud\Containers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use CSFCloud\KeyManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use CSFCloud\Resource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Httpful\Request; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  | class Container extends Resource { | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |     private $containerId; | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     private $statusCache; | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     public function __construct (KeyManager $km, string $id) { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |         parent::__construct($km); | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |         $this->containerId = $id; | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |         $this->UpdateStatus(); | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private function UpdateStatus () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $request = Request::get("https://api.csfcloud.com/container/" . urlencode($this->containerId) . "?key=" . urlencode($this->keymanager->GetServerKey()))->expectsText()->send(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 22 |  |  |         $this->statusCache = json_decode($request->body, true); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 23 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     public function GetId () : string { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         return $this->containerId; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     public function GetConfiguration () : array { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         return $this->statusCache["configuration"]; | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function SetConfiguration (array $cnf) { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 34 |  |  |         $this->statusCache["configuration"] = $cnf; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     public function UpdateChanges () { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         Request::put("https://api.csfcloud.com/container/" . urlencode($this->containerId) . "?key=" . urlencode($this->keymanager->GetServerKey())) | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             ->sendsJson()->body(json_encode($this->statusCache["configuration"]))->send(); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function GetContainerName () : string { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 43 |  |  |         return $this->statusCache["configuration"]["name"]; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     public function SetContainerName (string $newname) { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $this->statusCache["configuration"]["name"] = $newname; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public function IsRunning () : bool { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  |         return $this->statusCache["running"]; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function GetLastLogId () : ?string { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 55 |  |  |         return $this->statusCache["last_log"]; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                                                                
            
                                    
            
            
                | 58 |  |  |     public function GetLastLogWebSocketUrl () : string { | 
            
                                                                        
                                                                
            
                                    
            
            
                | 59 |  |  |         return "wss://dashboard-logs.csfcloud.com/log?id=" . $this->statusCache["last_log"]; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     public function Build () : string { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         $response = Request::get("https://api.csfcloud.com/container/" . urlencode($this->containerId) . "/build?key=" . urlencode($this->keymanager->GetServerKey()))->expectsJson()->send(); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         $this->statusCache["last_log"] = $response->body->run_id; | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         return $this->statusCache["last_log"]; | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public function Run () : string { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $response = Request::get("https://api.csfcloud.com/container/" . urlencode($this->containerId) . "/run?key=" . urlencode($this->keymanager->GetServerKey()))->expectsJson()->send(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $this->statusCache["last_log"] = $response->body->run_id; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  |         return $this->statusCache["last_log"]; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 74 |  |  | } |