Code Repeat

0

I'm having problem with code repetition in my CURL connection class, the class is being declared as follows:

<?php
if (!defined("ROOT_PATH")) define("ROOT_PATH", dirname(__FILE__, 2));

require_once ROOT_PATH."/helpers/Helper.php";

class PhabricatorModel
{
    const TOKEN = "api.token=api-...";

    public function add($parameters)
    {
        $channel = curl_init();
        curl_setopt($channel, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
        curl_setopt($channel, CURLOPT_URL, "https://pb-dc.com/api/maniphest.edit");
        curl_setopt($channel, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($channel, CURLOPT_POSTFIELDS, self::TOKEN.$parameters);
        curl_setopt($channel, CURLOPT_POST, 1);
        $result = curl_exec($channel);
        if (curl_errno($channel)) {
            return false;
        }

        curl_close($channel);

        return $result;
    }

    public function consultProject($project)
    {
        $channel = curl_init();
        curl_setopt($channel, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
        curl_setopt($channel, CURLOPT_URL, "https://pb-dc.com/api/project.query");
        curl_setopt($channel, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($channel, CURLOPT_POSTFIELDS, self::TOKEN."names[]=".$project);
        curl_setopt($channel, CURLOPT_POST, 1);
        $result = Helper::convert_array(json_decode(curl_exec($channel)));
        if (curl_errno($channel)) {
            return false;
        }

        curl_close($channel);

        return $result;
    }

    public function consultStory($story)
    {
        $channel = curl_init();
        curl_setopt($channel, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
        curl_setopt($channel, CURLOPT_URL, "https://pb-dc.com/api/maniphest.query");
        curl_setopt($channel, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($channel, CURLOPT_POSTFIELDS, self::TOKEN."status=status-open&fullText=".$story);
        curl_setopt($channel, CURLOPT_POST, 1);
        $result = Helper::convert_array(json_decode(curl_exec($channel)));
        if (curl_errno($channel)) {
            return false;
        }

        curl_close($channel);

        return $result;
    }
}

If I notice the same item as curl_init in all 3 methods and I wanted to remove this repeat, not only in curl_init but also elsewhere.

    
asked by anonymous 01.04.2018 / 03:55

1 answer

1

Treat the variable $channel as an attribute of the class and create a specific private method to initialize it with the values that already repeat therein, something like this:

class PhabricatorModel
{
        const TOKEN = "api.token=api-...";

        // o que é comum fica aqui...
        private function curl_init(){
            $this->channel = curl_init();
            curl_setopt($this->channel, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
            curl_setopt($this->channel, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($this->channel, CURLOPT_POST, 1);
        }

        // o que é específico fica num método próprio...
        public function add($parameters) {
            $this->curl_init();
            curl_setopt($this->channel, CURLOPT_URL, "https://pb-dc.com/api/maniphest.edit");
            curl_setopt($this->channel, CURLOPT_POSTFIELDS, self::TOKEN.$parameters);
            if (curl_errno($this->channel)){
                $result = false;
            } else {
                $result = curl_exec($this->channel);
            }
            curl_close($self->channel);
            return $result;
        }
        // alterei a lógica no final para evitar surpresas.

        // ...

And the same thing would be done in the other methods.

    
01.04.2018 / 13:55