Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
92.86% covered (success)
92.86%
13 / 14
CRAP
99.01% covered (success)
99.01%
100 / 101
Connection
0.00% covered (danger)
0.00%
0 / 1
92.86% covered (success)
92.86%
13 / 14
39
99.01% covered (success)
99.01%
100 / 101
 __construct(\aae\db\FunctionAPI $storageAPI)
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 initiateConnection($user, $connectionDetails)
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
6 / 6
 reciprocateConnection($user, $initConnectionId, $plainCode, $connectionDetails)
0.00% covered (danger)
0.00%
0 / 1
6.01
92.86% covered (success)
92.86%
13 / 14
 deleteInitiatedConnection($user)
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getOwnDetails($user, $connectionId)
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 getOtherDetails($user, $connectionId)
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 getOwnDisplayNameForConnection($user, $connectionId)
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getAll($user)
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 checkInitResponse($user, $initConnectionId)
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 deleteConnection($user, $connectionId)
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 _getFormattedConnections(&$connections)
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
 _getFormattedConnection(&$connection)
100.00% covered (success)
100.00%
1 / 1
5
100.00% covered (success)
100.00%
14 / 14
 _validateConnectionDetails($connectionDetails)
100.00% covered (success)
100.00%
1 / 1
15
100.00% covered (success)
100.00%
17 / 17
 _insertConnectionDetails($user, $initConnectionId, $connectionDetails, $dataFromInitiator = false)
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
27 / 27
<?php
/**
 *
 */
namespace aae\app {
    use \aae\db\FunctionAPI as FAPI;
    use \aae\std\std as std;
    /**
     * @author Axel Ancona Esselmann
     * @package aae\app
     */
    class Connection implements ConnectionInterface {
        private $_storageAPI;
        public function __construct(\aae\db\FunctionAPI $storageAPI) {
            $this->_storageAPI = $storageAPI;
        }
        public function initiateConnection($user, $connectionDetails) {
            $this->_validateConnectionDetails($connectionDetails);
            $plainCode    = $user->getCode(20);
            $codeHash     = $user->createPWHash($plainCode);
            $userEmail    = $user->getEmail();
            $connectionId = (int)$this->_storageAPI->initiateConnection($userEmail, $codeHash, $connectionDetails);
            return ["connectionId" => $connectionId, "plainCode" => $plainCode];
        }
        public function reciprocateConnection($user, $initConnectionId, $plainCode, $connectionDetails) {
            $userEmail = $user->getEmail();
            $codeHash  = $this->_storageAPI->getInitiatedConnectionCode($initConnectionId);
            $validCode = $user->verifyPWHash($plainCode, $codeHash);
            if ($validCode) {
                $connectionId = $this->_storageAPI->reciprocateConnection($userEmail, $initConnectionId, $plainCode);
                $initConnDet  = $this->_storageAPI->getInitiatorConnectionDetails($initConnectionId);
                if ($connectionId > 0 && strlen($initConnDet) > 0) {
                    $initDetInserted  = (bool)$this->_insertConnectionDetails($user, $connectionId, $initConnDet, true);
                    $recipDetInserted = (bool)$this->_insertConnectionDetails($user, $connectionId, $connectionDetails);
                    if ($initDetInserted && $recipDetInserted) {
                        return $connectionId;
                    }
                }
            }
            return false;
        }
        public function deleteInitiatedConnection($user) {
            return (bool)$this->_storageAPI->deleteInitiatedConnection($user->getEmail());
        }
        public function getOwnDetails($user, $connectionId) {
            $this->_storageAPI->setFetchMode(FAPI::FETCH_ASS_ARRAY | FAPI::FETCH_ONE_ROW);
            $connection = $this->_storageAPI->getOwnDetails($user->getEmail(), $connectionId);
            $this->_storageAPI->setFetchMode(FAPI::RESET);
            return $connection;
        }
        public function getOtherDetails($user, $connectionId) {
            $this->_storageAPI->setFetchMode(FAPI::FETCH_ASS_ARRAY | FAPI::FETCH_ONE_ROW);
            $connection = $this->_storageAPI->getOtherDetails($user->getEmail(), $connectionId);
            $this->_storageAPI->setFetchMode(FAPI::RESET);
            return $this->_getFormattedConnection($connection);
        }
        public function getOwnDisplayNameForConnection($user, $connectionId) {
            return $this->_storageAPI->getOwnDisplayNameForConnection($user->getEmail(), $connectionId);
        }
        public function getAll($user) {
            $this->_storageAPI->setFetchMode(FAPI::FETCH_ASS_ARRAY);
            $connections = $this->_storageAPI->getAllConnections($user->getEmail());
            $this->_storageAPI->setFetchMode(FAPI::RESET);
            return $this->_getFormattedConnections($connections);
        }
        public function checkInitResponse($user, $initConnectionId) {
            return (bool)$this->_storageAPI->checkInitResponse($user->getEmail(), $initConnectionId);
        }
        public function deleteConnection($user, $connectionId) {
            return (bool)$this->_storageAPI->deleteConnection($user->getEmail(), $connectionId);
        }
        protected function _getFormattedConnections(&$connections) {
            $result = [];
            for ($i=0; $i < count($connections); $i++) {
                $result[] = $this->_getFormattedConnection($connections[$i]);
            }
            return $result;
        }
        protected function _getFormattedConnection(&$connection) {
            $result = [];
            $name = "CORRUPTED_DATA";
            if ((bool)$connection["show_alias"]) {
                $name = $connection["alias"];
            } else if ((bool)$connection["show_user_name"]) {
                $name = $connection["user_name"];
            } else if ((bool)$connection["show_real_name"]) {
                $name = $connection["user_name"]; // TODO: THIS IS PLACEHOLDER TEXT
            } else throw new \Exception("Error retrieving user data", 205151131);
            $result["name"]            = $name;
            $result["email"]           = ((bool)$connection["show_email"]) ? $connection["user_email"] : "";
            $result["connection_id"]   = (int)$connection["connection_id"];
            $result["can_be_messaged"] = (bool)$connection["can_be_messaged"];
            return $result;
        }
        protected function _validateConnectionDetails($connectionDetails) {
            $assoc = json_decode($connectionDetails, true);
            if (!is_array($assoc)) throw new \aae\std\ValidationException("Connection details not in JSON form", 1113142150);
            if (!(
                array_key_exists("show_real_name", $assoc) ||
                array_key_exists("show_user_name", $assoc) ||
                array_key_exists("show_alias", $assoc)
            )) throw new \aae\std\ValidationException("Either show_real_name, show_user_name or show_alias has to exist in connection details", 1113142151);
            if (
                !(
                    (array_key_exists("show_real_name", $assoc) && (bool)$assoc["show_real_name"] === true) ||
                    (array_key_exists("show_user_name", $assoc) && (bool)$assoc["show_user_name"] === true) ||
                    (array_key_exists("show_alias", $assoc)     && (bool)$assoc["show_alias"]     === true)
                )
            ) throw new \aae\std\ValidationException("Either show_real_name, show_user_name or show_alias must be true", 1113142152);
            if (
                array_key_exists("show_alias", $assoc) &&
                (bool)$assoc["show_alias"] === true    &&
                (!array_key_exists("alias", $assoc)    ||
                strlen($assoc["alias"]) < 1)
            ) throw new \aae\std\ValidationException("When using show_alias, alias must be given", 1113142153);
            return true;
        }
        protected function _insertConnectionDetails($user, $initConnectionId, $connectionDetails, $dataFromInitiator = false) {
            $assoc = json_decode($connectionDetails);
            $details = [
                "alias"           => "",
                "can_be_messaged" => false,
                "show_real_name"  => false,
                "show_user_name"  => false,
                "show_alias"      => false,
                "show_email"      => false,
                "show_phone"      => false,
                "show_address"    => false,
            ];
            foreach ($assoc as $key => $value) {
                $details[$key] = $value;
            }
            $success = (bool)$this->_storageAPI->insertConnectionDetails(
                (int)$dataFromInitiator,
                $initConnectionId,
                $user->getEmail(),
                $details["alias"],
                (int)$details["can_be_messaged"],
                (int)$details["show_real_name"],
                (int)$details["show_user_name"],
                (int)$details["show_alias"],
                (int)$details["show_email"],
                (int)$details["show_phone"],
                (int)$details["show_address"]
            );
            return $success;
        }
    }
}