Code Duplication    Length = 35-35 lines in 2 locations

src/Serializers/EcSerializer.php 1 location

@@ 105-139 (lines=35) @@
102
   * @param string $method
103
   *   Should be a table_name, and will be converted to camelCase.
104
   */
105
  public function __call($method, $args) {
106
    self::$potentialDataTables = \Drupal\esdportal_api\EcDataUtils::getDataTablesWithProgramIds();
107
    self::$potentialDataTableNames = \Drupal\esdportal_api\EcDataUtils::extractDataTableNames(self::$potentialDataTables);
108
109
    return function ($ec, $include, $included) use ($method) {
110
      // The actual called method is underscore-separated...
111
      $table_name = $method;
112
113
      // ... But we want a method that is camelCased.
114
      $camelized_method = \Drupal\esdportal_api\EcDataUtils::underscoreToCamel($table_name);
115
116
      $class_name = 'Drupal\\esdportal_api\\Serializers\\' . $camelized_method . 'Serializer';
117
118
      // Legit data table name?
119
      if (!in_array($table_name, self::$potentialDataTableNames)) {
120
        return NULL;
121
      }
122
123
      $serializer = new $class_name($included);
124
125
      if (!$ec->program_id) {
126
        return NULL;
127
      }
128
129
      if (!$ec->{$table_name}) {
130
        return NULL;
131
      }
132
133
      $datum = $serializer->resource($include ? $ec->{$table_name} : $ec->program_id);
134
135
      $link = new Link($datum);
136
137
      return $link;
138
    };
139
  }
140
141
}
142

src/Serializers/SchoolSerializer.php 1 location

@@ 108-142 (lines=35) @@
105
   * @param string $method
106
   *   Should be a table_name, and will be converted to camelCase.
107
   */
108
  public function __call($method, $args) {
109
    self::$potentialDataTables = \Drupal\esdportal_api\EcDataUtils::getDataTablesWithBcodes();
110
    self::$potentialDataTableNames = \Drupal\esdportal_api\EcDataUtils::extractDataTableNames(self::$potentialDataTables);
111
112
    return function ($school, $include, $included) use ($method) {
113
      // The actual called method is underscore-separated...
114
      $table_name = $method;
115
116
      // ... But we want a method that is camelCased.
117
      $camelized_method = \Drupal\esdportal_api\EcDataUtils::underscoreToCamel($table_name);
118
119
      $class_name = 'Drupal\\esdportal_api\\Serializers\\' . $camelized_method . 'Serializer';
120
121
      // Legit data table name?
122
      if (!in_array($table_name, self::$potentialDataTableNames)) {
123
        return NULL;
124
      }
125
126
      $serializer = new $class_name($included);
127
128
      if (!$school->bcode) {
129
        return NULL;
130
      }
131
132
      if (!$school->{$table_name}) {
133
        return NULL;
134
      }
135
136
      $datum = $serializer->resource($include ? $school->{$table_name} : $school->bcode);
137
138
      $link = new Link($datum);
139
140
      return $link;
141
    };
142
  }
143
144
}
145