Overview

Namespaces

  • Contrib
    • Bundle
      • CoverallsBundle
        • Console
        • Entity
      • CoverallsV1Bundle
        • Api
        • Collector
        • Command
        • Config
        • Entity
          • Git
    • Component
      • File
      • Log
      • System
        • Git
  • Guzzle
    • Batch
      • Exception
    • Cache
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
      • QueryAggregator
    • Inflection
    • Iterator
    • Log
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Async
      • Backoff
      • Cache
      • Cookie
        • CookieJar
        • Exception
      • CurlAuth
      • ErrorResponse
        • Exception
      • History
      • Log
      • Md5
      • Mock
      • Oauth
    • Service
      • Builder
      • Command
        • Factory
        • LocationVisitor
          • Request
          • Response
      • Description
      • Exception
      • Resource
    • Stream
  • PHP
  • Psr
    • Log
  • Symfony
    • Component
      • Config
        • Definition
          • Builder
          • Exception
        • Exception
        • Loader
        • Resource
        • Util
      • Console
        • Command
        • Formatter
        • Helper
        • Input
        • Output
        • Tester
      • EventDispatcher
        • Debug
      • Finder
        • Adapter
        • Comparator
        • Exception
        • Expression
        • Iterator
        • Shell
      • Stopwatch
      • Yaml
        • Exception

Classes

  • CiEnvVarsCollector
  • CloverXmlCoverageCollector
  • GitInfoCollector
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  1: <?php
  2: namespace Contrib\Bundle\CoverallsV1Bundle\Collector;
  3: 
  4: use Contrib\Bundle\CoverallsV1Bundle\Entity\Git\Remote;
  5: use Contrib\Bundle\CoverallsV1Bundle\Entity\Git\Commit;
  6: use Contrib\Bundle\CoverallsV1Bundle\Entity\Git\Git;
  7: use Contrib\Component\System\Git\GitCommand;
  8: 
  9: /**
 10:  * Git repository info collector.
 11:  *
 12:  * @author Kitamura Satoshi <with.no.parachute@gmail.com>
 13:  */
 14: class GitInfoCollector
 15: {
 16:     /**
 17:      * Git command.
 18:      *
 19:      * @var GitCommand
 20:      */
 21:     protected $command;
 22: 
 23:     /**
 24:      * Constructor.
 25:      *
 26:      * @param GitCommand $gitCommand Git command
 27:      */
 28:     public function __construct(GitCommand $command)
 29:     {
 30:         $this->command = $command;
 31:     }
 32: 
 33:     // API
 34: 
 35:     /**
 36:      * Collect git repository info.
 37:      *
 38:      * @return \Contrib\Bundle\CoverallsV1Bundle\Entity\Git\Git
 39:      */
 40:     public function collect()
 41:     {
 42:         $branch  = $this->collectBranch();
 43:         $commit  = $this->collectCommit();
 44:         $remotes = $this->collectRemotes();
 45: 
 46:         return new Git($branch, $commit, $remotes);
 47:     }
 48: 
 49:     // internal method
 50: 
 51:     /**
 52:      * Collect branch name.
 53:      *
 54:      * @return string
 55:      * @throws \RuntimeException
 56:      */
 57:     protected function collectBranch()
 58:     {
 59:         $branchesResult = $this->command->getBranches();
 60: 
 61:         foreach ($branchesResult as $result) {
 62:             if (strpos($result, '* ') === 0) {
 63:                 $exploded = explode('* ', $result, 2);
 64: 
 65:                 return $exploded[1];
 66:             }
 67:         }
 68: 
 69:         throw new \RuntimeException();
 70:     }
 71: 
 72:     /**
 73:      * Collect commit info.
 74:      *
 75:      * @return \Contrib\Bundle\CoverallsV1Bundle\Entity\Git\Commit
 76:      * @throws \RuntimeException
 77:      */
 78:     protected function collectCommit()
 79:     {
 80:         $commitResult = $this->command->getHeadCommit();
 81: 
 82:         if (count($commitResult) !== 6 || array_keys($commitResult) !== range(0, 5)) {
 83:             throw new \RuntimeException();
 84:         }
 85: 
 86:         $commit = new Commit();
 87: 
 88:         return $commit
 89:         ->setId($commitResult[0])
 90:         ->setAuthorName($commitResult[1])
 91:         ->setAuthorEmail($commitResult[2])
 92:         ->setCommitterName($commitResult[3])
 93:         ->setCommitterEmail($commitResult[4])
 94:         ->setMessage($commitResult[5]);
 95:     }
 96: 
 97:     /**
 98:      * Collect remotes info.
 99:      *
100:      * @return \Contrib\Bundle\CoverallsV1Bundle\Entity\Git\Remote[]
101:      * @throws \RuntimeException
102:      */
103:     protected function collectRemotes()
104:     {
105:         $remotesResult = $this->command->getRemotes();
106: 
107:         if (count($remotesResult) === 0) {
108:             throw new \RuntimeException();
109:         }
110: 
111:         // parse command result
112:         $results = array();
113: 
114:         foreach ($remotesResult as $result) {
115:             if (strpos($result, ' ') !== false) {
116:                 list($remote) = explode(' ', $result, 2);
117: 
118:                 $results[] = $remote;
119:             }
120:         }
121: 
122:         // filter
123:         $results = array_unique($results);
124: 
125:         // create Remote instances
126:         $remotes = array();
127: 
128:         foreach ($results as $result) {
129:             if (strpos($result, "\t") !== false) {
130:                 list($name, $url) = explode("\t", $result, 2);
131: 
132:                 $remote = new Remote();
133:                 $remotes[] = $remote->setName($name)->setUrl($url);
134:             }
135:         }
136: 
137:         return $remotes;
138:     }
139: 
140:     // accessor
141: 
142:     /**
143:      * Return git command.
144:      *
145:      * @return \Contrib\Component\System\Git\GitCommand
146:      */
147:     public function getCommand()
148:     {
149:         return $this->command;
150:     }
151: }
152: 
php-coveralls API documentation generated by ApiGen 2.8.0