1
+ <?php
2
+
3
+ namespace Nitrapi \SSHKeys ;
4
+
5
+ use Nitrapi \Nitrapi ;
6
+
7
+ class SSHKey {
8
+
9
+ /**
10
+ * @var Nitrapi
11
+ */
12
+ private $ api ;
13
+
14
+ /**
15
+ * @var array
16
+ */
17
+ private $ data ;
18
+
19
+ public function __construct (Nitrapi $ api , array $ data ) {
20
+ $ this ->api = $ api ;
21
+ $ this ->data = $ data ;
22
+ $ this ->data ['full_public_key ' ] = $ this ->data ['type ' ] . ' ' . $ this ->data ['public_key ' ] . ' ' . $ this ->data ['comment ' ];
23
+ }
24
+
25
+ /**
26
+ * Returns the full SSH public key
27
+ *
28
+ * @return string
29
+ */
30
+ public function getPublicKey ()
31
+ {
32
+ return $ this ->data ['full_public_key ' ];
33
+ }
34
+
35
+ /**
36
+ * Updates the existing SSH public key
37
+ *
38
+ * @return SSHKey
39
+ */
40
+ public function setPublicKey ($ key )
41
+ {
42
+ $ this ->data ['full_public_key ' ] = $ key ;
43
+ $ this ->doUpdate ();
44
+ return $ this ;
45
+ }
46
+
47
+ /**
48
+ * Returns true if the key is enabled
49
+ *
50
+ * @return bool
51
+ */
52
+ public function isEnabled ()
53
+ {
54
+ return (bool )$ this ->data ['enabled ' ];
55
+ }
56
+
57
+ /**
58
+ * Returns true if the key is enabled
59
+ *
60
+ * @return SSHKey
61
+ */
62
+ public function setEnabled ($ enabled = true )
63
+ {
64
+ $ this ->data ['enabled ' ] = $ enabled ;
65
+ $ this ->doUpdate ();
66
+ return $ this ;
67
+ }
68
+
69
+ /**
70
+ * Deletes this SSH public key
71
+ *
72
+ * @return bool
73
+ */
74
+ public function doDelete ()
75
+ {
76
+ $ url = "user/ssh_keys/ " . $ this ->data ['id ' ];
77
+ $ this ->api ->dataDelete ($ url );
78
+
79
+ return true ;
80
+ }
81
+
82
+ /**
83
+ * Updates this SSH public key in database
84
+ *
85
+ * @return $this
86
+ */
87
+ private function doUpdate ()
88
+ {
89
+ $ url = "user/ssh_keys/ " . $ this ->data ['id ' ];
90
+ $ this ->api ->dataPost ($ url , [
91
+ 'key ' => $ this ->getPublicKey (),
92
+ 'enabled ' => ($ this ->isEnabled () ? 'true ' : 'false ' )
93
+ ]);
94
+
95
+ return $ this ;
96
+ }
97
+
98
+ }
0 commit comments