4f5034c52cf4b94404af63987e78e2c61b6be640
[phorkie.git] / src / phorkie / Repository / Remote.php
1 <?php
2 namespace phorkie;
3
4 class Repository_Remote
5 {
6     protected $arConfig;
7     protected $name;
8
9     public function __construct($name, $arConfig)
10     {
11         $this->name = $name;
12         $this->arConfig = $arConfig;
13     }
14
15
16     public function getTitle()
17     {
18         if (isset($this->arConfig['title'])) {
19             return $this->arConfig['title'];
20         }
21         if ($this->isLocal()) {
22             $local = $this->getLocalRepository();
23             if ($local !== null) {
24                 return $local->getTitle();
25             }
26             return 'deleted local paste';
27         }
28
29         return 'untitled repository';
30     }
31
32     public function getCloneURL()
33     {
34         if ($this->isLocal()) {
35             $local = $this->getLocalRepository();
36             if ($local !== null) {
37                 return $local->getCloneURL();
38             }
39         }
40
41         return $this->arConfig['url'];
42     }
43
44     public function getWebURL()
45     {
46         if (isset($this->arConfig['homepage'])) {
47             return $this->arConfig['homepage'];
48         }
49
50         if ($this->isLocal()) {
51             $local = $this->getLocalRepository();
52             if ($local !== null) {
53                 return $local->getLink('display');
54             }
55         }
56
57         return null;
58     }
59
60     /**
61      * Tells you if this remote repository is a paste on the local server
62      *
63      * @return boolean True of false
64      */
65     public function isLocal()
66     {
67         return isset($this->arConfig['url'])
68             && $this->arConfig['url']{0} == '/';
69     }
70
71     /**
72      * If this remote is a local paste, then we'll get the repository object
73      * returned
74      *
75      * @return Repository Repository object or NULL
76      */
77     public function getLocalRepository()
78     {
79         if (!file_exists($this->arConfig['url'] . '/config')) {
80             return null;
81         }
82         $dir = basename($this->arConfig['url']);
83         if (substr($dir, -4) != '.git') {
84             //phorks are bare repositories "123.git"
85             return null;
86         }
87         $repo = new Repository();
88         $repo->loadById(substr($dir, 0, -4));
89         return $repo;
90     }
91
92 }
93
94 ?>